pub struct BroadcastBus<E> { /* private fields */ }Expand description
A typed publish/subscribe bus that broadcasts each event to all subscribers.
Every handle can both publish and receive, and it receives what it publishes
itself. Use subscribe to add an independent
consumer; an event published after a handle subscribes is delivered to it. A
subscriber only sees events published after it subscribed, mirroring a live
pub/sub channel.
Each handle holds a publisher of its own, so the bus stays open while any
handle exists and next_event on a BroadcastBus
never returns None: it waits until an event arrives. A wait given up, by a
timeout or a select!, takes no event.
§Examples
use pamoja_core::EventBus;
use pamoja_bus::BroadcastBus;
let bus = BroadcastBus::new(16);
let mut subscriber = bus.subscribe();
bus.publish("reading").await?;
assert_eq!(subscriber.next_event().await?, Some("reading"));Implementations§
Source§impl<E: Clone> BroadcastBus<E>
impl<E: Clone> BroadcastBus<E>
Sourcepub fn new(capacity: usize) -> Self
pub fn new(capacity: usize) -> Self
Creates a bus buffering unread events for each subscriber.
§Arguments
capacity- the per-subscriber buffer depth, rounded up to the next power of two; a subscriber further behind than the buffer holds drops the events it missed. Values below one are raised to one, and values aboveMAX_CAPACITYare lowered to it.
§Returns
A bus with one handle that can publish and receive.
Sourcepub fn subscribe(&self) -> Self
pub fn subscribe(&self) -> Self
Creates another handle to the same bus with its own independent subscription.
§Returns
A handle that receives events published after this call and can also publish.
Sourcepub fn publisher(&self) -> EventPublisher<E>
pub fn publisher(&self) -> EventPublisher<E>
Creates a publish-only handle to the same bus.
§Returns
A handle that publishes without borrowing this one, so it can announce from
another task while this handle waits in next_event.