Skip to main content

EventPublisher

Struct EventPublisher 

Source
pub struct EventPublisher<E> { /* private fields */ }
Expand description

A publish-only handle to a bus.

A publisher has no buffer to fill, so a part that only announces holds one rather than a BroadcastBus. Cloning it is cheap, and publishing never waits: a subscriber that falls behind loses events rather than holding up the publisher.

§Examples

use pamoja_core::EventBus;
use pamoja_bus::EventPublisher;

let power = EventPublisher::new(8);
let mut control = power.subscribe();
let mut logger = power.subscribe();
assert_eq!(power.publish("battery.low"), 2);
assert_eq!(control.next_event().await?, Some("battery.low"));
assert_eq!(logger.next_event().await?, Some("battery.low"));

Implementations§

Source§

impl<E: Clone> EventPublisher<E>

Source

pub fn new(capacity: usize) -> Self

Creates a bus with no subscribers yet, and a publisher on it.

§Arguments
  • capacity - the per-subscriber buffer depth, rounded up to the next power of two. Values below one are raised to one, and values above MAX_CAPACITY are lowered to it.
§Returns

A publisher to take subscribers from and publish to them.

Source

pub fn publish(&self, event: E) -> usize

Hands an event to every current subscriber.

§Arguments
  • event - the event to broadcast.
§Returns

How many subscribers the event was handed to. An event published while no one is subscribed is dropped, and the count is 0.

Source

pub fn subscribe(&self) -> BroadcastBus<E>

Subscribes to the bus.

§Returns

A handle that receives events published after this call and can also publish.

Source

pub fn publisher(&self) -> EventPublisher<E>

Takes another publisher on the same bus, for another part that announces.

§Returns

A publisher with a lifetime of its own, the same as a clone of this one.

Trait Implementations§

Source§

impl<E: Clone> Clone for EventPublisher<E>

Source§

fn clone(&self) -> EventPublisher<E>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<E> Freeze for EventPublisher<E>

§

impl<E> RefUnwindSafe for EventPublisher<E>

§

impl<E> Send for EventPublisher<E>
where E: Send,

§

impl<E> Sync for EventPublisher<E>
where E: Send,

§

impl<E> Unpin for EventPublisher<E>

§

impl<E> UnsafeUnpin for EventPublisher<E>

§

impl<E> UnwindSafe for EventPublisher<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.