add event_bus_typed
This commit is contained in:
+44
-44
@@ -1,49 +1,49 @@
|
||||
/// Downcasts an [`AnyEvent`] to one or more concrete types and executes
|
||||
/// the matching closure if the type matches.
|
||||
///
|
||||
/// Non-matching branches are silently ignored.
|
||||
///
|
||||
/// # Syntax
|
||||
/// ```text
|
||||
/// match_event!(evt, Type1 => |val| { ... }, Type2 => |val| { ... })
|
||||
/// ```
|
||||
///
|
||||
/// # Example
|
||||
/// ```rust,no_run
|
||||
/// # use std::sync::Arc;
|
||||
/// # use oxspeak_server_lib::event_bus::EventBus;
|
||||
/// # use oxspeak_server_lib::match_event;
|
||||
/// # #[derive(Clone, Debug)] struct User { name: String }
|
||||
/// # #[derive(Clone, Debug)] struct UdpMetric { value: f32 }
|
||||
/// # let bus = Arc::new(EventBus::new());
|
||||
/// # tokio_test::block_on(async {
|
||||
/// let mut rx = bus.on_raw("user-connected");
|
||||
/// bus.emit("user-connected", User { name: "Alice".into() });
|
||||
///
|
||||
/// if let Ok(evt) = rx.recv().await {
|
||||
/// match_event!(evt,
|
||||
/// User => |u| println!("User: {:?}", u),
|
||||
/// UdpMetric => |m| println!("Metric: {:?}", m),
|
||||
/// );
|
||||
/// }
|
||||
/// # });
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! match_event {
|
||||
($evt:expr, $($type:ty => $handler:expr),+ $(,)?) => {
|
||||
$(
|
||||
if let Some(val) = ($evt).downcast_ref::<$type>() {
|
||||
($handler)(val.clone());
|
||||
} else
|
||||
)+
|
||||
{
|
||||
// No matching type → silently ignored
|
||||
}
|
||||
};
|
||||
}
|
||||
//! # 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;
|
||||
pub use bus::{AnyEvent, EventBus, Scope, ScopeValue};
|
||||
mod handler;
|
||||
|
||||
pub use bus::{DEFAULT_CAPACITY, Event, EventBus};
|
||||
pub use handler::{AsyncHandler, AsyncHandlerWith};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
Reference in New Issue
Block a user