Skip to main content

EventBus

Struct EventBus 

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

Generic event bus that dispatches events through staged handlers.

Handlers register for specific event types at specific stages. During dispatch, handlers run in (Stage, priority) order. If any Validate handler cancels the event, Process and Post handlers are skipped.

The bus is Send + Sync and can be shared via Arc across connection tasks. Registration happens at startup; dispatch happens per-task.

Implementations§

Source§

impl EventBus

Source

pub fn new() -> Self

Creates an empty event bus with no registered handlers.

Source

pub fn on<E>( &mut self, stage: Stage, priority: i32, handler: impl Fn(&mut E, &dyn Context) + Send + Sync + 'static, )
where E: Event + 'static,

Registers a handler for a specific event type at a given stage.

The handler receives a mutable reference to the concrete event and a &dyn Context reference. Lower priority values run first within the same stage.

E must be 'static for type erasure via Any. The handler closure must be Send + Sync since the bus is shared across connection tasks.

Source

pub fn dispatch<E>(&self, event: &mut E, ctx: &dyn Context)
where E: Event + 'static,

Dispatches a concrete event through all registered handlers.

Runs handlers in (Stage, priority) order. If the event is cancelled during Validate, Process and Post are skipped.

Source

pub fn dispatch_dyn(&self, event: &mut dyn Event, ctx: &dyn Context)

Dispatches a type-erased event using its runtime TypeId.

Used when the concrete event type is not known at the call site (e.g., Box<dyn Event> from packet_to_event).

Source

pub fn event_type_count(&self) -> usize

Returns the number of event types that have registered handlers.

Source

pub fn handler_count(&self) -> usize

Returns the total number of registered handler entries.

Trait Implementations§

Source§

impl Default for EventBus

Source§

fn default() -> Self

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.