Skip to main content

BroadcastBus

Struct BroadcastBus 

Source
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>

Source

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 above MAX_CAPACITY are lowered to it.
§Returns

A bus with one handle that can publish and receive.

Source

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.

Source

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.

Source

pub fn missed(&self) -> u64

Counts the events this handle lost by falling behind.

§Returns

How many events were dropped from this handle’s buffer before it read them, since it subscribed.

Trait Implementations§

Source§

impl<E: Clone> EventBus for BroadcastBus<E>

Source§

type Event = E

The event type carried by this bus.
Source§

async fn publish(&self, event: Self::Event) -> Result<()>

Publishes an event to every current subscriber. Read more
Source§

async fn next_event(&mut self) -> Result<Option<Self::Event>>

Awaits the next event for this subscriber. Read more

Auto Trait Implementations§

§

impl<E> Freeze for BroadcastBus<E>

§

impl<E> RefUnwindSafe for BroadcastBus<E>

§

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

§

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

§

impl<E> Unpin for BroadcastBus<E>

§

impl<E> UnsafeUnpin for BroadcastBus<E>

§

impl<E> UnwindSafe for BroadcastBus<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> 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, 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.