Expand description
§tokio-events
A modern, type-safe async event bus for Rust applications built on Tokio.
§Features
- Type-safe event publishing and subscription
- High performance async-first design built on Tokio
- Flexible subscription management
- Thread-safe by default
§Quick Example
use tokio_events::{Event, EventBus};
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct UserRegistered {
user_id: u64,
email: String,
}
impl Event for UserRegistered {
fn event_type() -> &'static str {
"UserRegistered"
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create event bus
let bus = EventBus::builder().build().await?;
// Subscribe to events
let handle = bus.subscribe(|event: UserRegistered| async move {
println!("New user registered: {}", event.email);
}).await?;
// Publish events
bus.publish(UserRegistered {
user_id: 123,
email: "user@example.com".to_string(),
}).await?;
// Unsubscribe when done
bus.unsubscribe(handle).await?;
Ok(())
}Re-exports§
pub use bus::EventBus;pub use bus::EventBusBuilder;pub use error::Error;pub use error::Result;pub use event::Event;pub use event::EventEnvelope;pub use event::EventMetadata;pub use event::EventPriority;pub use event::HasPriority;pub use subscription::EventHandler;pub use subscription::SubscriptionHandle;
Modules§
- bus
- The main event bus implementation The main EventBus implementation.
- dispatcher
- Event dispatcher for routing events Event dispatcher for routing events to handlers.
- error
- Error types and result aliases Error types for the tokio-events library.
- event
- Core event system traits and types Core event system traits and types.
- global
- Global singleton event bus access.
- prelude
- Prelude module for convenient imports
- registry
- Event registry for type-to-subscriber mapping Event registry for mapping event types to subscribers.
- subscription
- Subscription management for event handlers Subscription management for event handlers.
Derive Macros§
- Event
macros