Struct EventBus

Source
pub struct EventBus { /* private fields */ }
Expand description

A tiny pub/sub event bus

§Performance and Locking

While the event bus is not entirely lock-free, blocking write-locks are only used on registry updates; i.e. Self::subscribe and Self::shrink_to_fit. Event publication uses a non-blocking read-lock, and event listening and receiption is completely lock-free. In normal scenarios with moderate subscriber fluctuation, event passing is largely independent on mutex performance and only affected by the amount of event types and subscribers.

§Memory Allocation and Publication

The event bus itself only (de-)allocates memory on registry updates; i.e. Self::subscribe and Self::shrink_to_fit.

However, during event publication events are cloned for each subscriber to achieve strong decoupling. If Clone is not cheap for your event type or you expect a large number of subscribers, consider wrapping the event into an Arc or similar referencing types to keep the performance and memory impact low.

§Many Subscribers

Due to the type abstraction layer, each new event needs to be checked against every subscriber to see if the subscriber can handle this event. While this check is cheap, it may accumulate if you have a very high event-throughput with lots of subscribers.

Implementations§

Source§

impl EventBus

Source

pub const fn new() -> Self

Creates a new event bus

Source

pub fn publish<T>(&self, event: T) -> usize
where T: Send + Clone + 'static,

Publishes an event to all registered subscribers and returns the amount of subscribers addressed

Source

pub fn subscribe<T>(&self, backlog: usize) -> Subscription<T>
where T: Send + Clone + 'static,

Subscribes to a given event type

Source

pub fn subscribe_where<T, F>( &self, backlog: usize, filter_map: F, ) -> Subscription<T>
where T: Send + Clone + 'static, F: Fn(&dyn Any) -> Option<T> + Send + Sync + 'static,

Creates an aggregate subscriber for any event type X where aggregate(&X) => Some(T)

§Performance

Please note that the mapping function is called for event for each subscriber to see if the event can be delivered to the subscriber. If the mapping is expensive, it is recommended to add an early-abort check before the real mapping begins to quickly reject invalid types.

§See Also

See also crate::where_into and crate::where_try_into to create mappers for Into and TryInto convertible types.

Source

pub fn shrink_to_fit(&self)

Shrinks the allocated capacity as much as possible

Trait Implementations§

Source§

impl Debug for EventBus

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EventBus

Source§

fn default() -> EventBus

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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.