pub struct MessageBus { /* private fields */ }Expand description
Dispatches commands (one handler) and events (fan-out) in-process.
§Examples
use serenade_messenger::{
Command, CommandHandler, Message, MessageBus, MessengerError,
};
struct EnqueueJob;
impl Message for EnqueueJob {
const NAME: &'static str = "job.enqueue";
}
impl Command for EnqueueJob {}
struct EnqueueJobHandler;
impl CommandHandler<EnqueueJob> for EnqueueJobHandler {
fn handle(&self, _command: &EnqueueJob) -> Result<(), MessengerError> {
Ok(())
}
}
let mut bus = MessageBus::new();
bus.register_command(EnqueueJobHandler).expect("register");
bus.dispatch_command(&EnqueueJob).expect("dispatch");Implementations§
Source§impl MessageBus
impl MessageBus
Sourcepub fn new() -> MessageBus
pub fn new() -> MessageBus
Creates an empty bus.
Sourcepub fn add_middleware(&mut self, middleware: impl Middleware + 'static)
pub fn add_middleware(&mut self, middleware: impl Middleware + 'static)
Appends a middleware layer (first registered runs outermost).
Sourcepub fn middleware_count(&self) -> usize
pub fn middleware_count(&self) -> usize
Number of registered middleware layers.
Sourcepub fn register_command<C, H>(
&mut self,
handler: H,
) -> Result<(), MessengerError>where
C: Command,
H: CommandHandler<C> + 'static,
pub fn register_command<C, H>(
&mut self,
handler: H,
) -> Result<(), MessengerError>where
C: Command,
H: CommandHandler<C> + 'static,
Registers the sole command handler for C::NAME.
§Errors
Returns MessengerError::DuplicateCommand when a handler already exists.
Sourcepub fn register_event<E, H>(&mut self, handler: H)where
E: Event,
H: EventHandler<E> + 'static,
pub fn register_event<E, H>(&mut self, handler: H)where
E: Event,
H: EventHandler<E> + 'static,
Appends an event handler for E::NAME (fan-out; order is registration order).
Sourcepub fn dispatch_command<C>(&self, command: &C) -> Result<(), MessengerError>where
C: Command,
pub fn dispatch_command<C>(&self, command: &C) -> Result<(), MessengerError>where
C: Command,
Dispatches command through the middleware pipeline to its handler.
§Errors
Returns MessengerError::UnknownCommand when no handler is registered, a
middleware rejection, or the handler’s MessengerError.
Sourcepub fn dispatch_event<E>(&self, event: &E) -> Result<(), MessengerError>where
E: Event,
pub fn dispatch_event<E>(&self, event: &E) -> Result<(), MessengerError>where
E: Event,
Dispatches event through middleware, then to every registered handler for E::NAME.
Missing handlers are a no-op after middleware. All matching handlers run even if one fails; the first error is returned.
§Errors
Returns a middleware rejection or the first MessengerError from a handler.
Sourcepub fn command_count(&self) -> usize
pub fn command_count(&self) -> usize
Number of registered command handlers.
Sourcepub fn event_handler_count(&self) -> usize
pub fn event_handler_count(&self) -> usize
Number of registered event handler callbacks (across all event names).
Trait Implementations§
Source§impl Clone for MessageBus
impl Clone for MessageBus
Source§fn clone(&self) -> MessageBus
fn clone(&self) -> MessageBus
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Default for MessageBus
impl Default for MessageBus
Source§fn default() -> MessageBus
fn default() -> MessageBus
Auto Trait Implementations§
impl !RefUnwindSafe for MessageBus
impl !UnwindSafe for MessageBus
impl Forget for MessageBus
impl Freeze for MessageBus
impl Move for MessageBus
impl Send for MessageBus
impl Sync for MessageBus
impl Unpin for MessageBus
impl UnsafeUnpin for MessageBus
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more