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

§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 up to capacity unread events per subscriber.

§Arguments
  • capacity - the per-subscriber buffer depth; a subscriber further behind than this drops the events it missed. Values below one are raised to one.
§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.

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.