50 lines
1.4 KiB
Rust
50 lines
1.4 KiB
Rust
//! # event_bus
|
|
//!
|
|
//! A strongly-typed, high-performance in-memory event bus for Tokio.
|
|
//!
|
|
//! ## Overview
|
|
//!
|
|
//! Unlike string/topic-based event buses, `event_bus` routes events using
|
|
//! their concrete Rust types ([`std::any::TypeId`]).
|
|
//!
|
|
//! - **Strong typing**: No string keys required for event types, no manual `match_event!`
|
|
//! macros, and no runtime downcasting (`downcast_ref`) inside the subscriber loops.
|
|
//! - **Ergonomic async subscribers**: Handlers can be registered with clean turbofish syntax:
|
|
//! `bus.on_async::<MessageUpdatedEvent>(|event| async move { ... })`.
|
|
//! - **Targeted wake-up**: Tokio broadcast channels are isolated per event type.
|
|
//!
|
|
//! ## Example
|
|
//!
|
|
//! ```rust,no_run
|
|
//! use event_bus::EventBus;
|
|
//!
|
|
//! #[derive(Clone, Debug, PartialEq)]
|
|
//! struct MessageCreatedEvent {
|
|
//! content: String,
|
|
//! }
|
|
//!
|
|
//! #[tokio::main]
|
|
//! async fn main() {
|
|
//! let bus = EventBus::new();
|
|
//!
|
|
//! // Async subscriber
|
|
//! bus.on_async::<MessageCreatedEvent>(|event| async move {
|
|
//! println!("Received message: {}", event.content);
|
|
//! });
|
|
//!
|
|
//! // Emit event
|
|
//! bus.emit(MessageCreatedEvent {
|
|
//! content: "Hello from typed event bus!".into(),
|
|
//! });
|
|
//! }
|
|
//! ```
|
|
|
|
mod bus;
|
|
mod handler;
|
|
|
|
pub use bus::{DEFAULT_CAPACITY, Event, EventBus};
|
|
pub use handler::{AsyncHandler, AsyncHandlerWith};
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|