Skip to main content

tokio_events/
lib.rs

1//! # tokio-events
2//!
3//! A modern, type-safe async event bus for Rust applications built on Tokio.
4//!
5//! ## Features
6//!
7//! - **Type-safe** event publishing and subscription
8//! - **High performance** async-first design built on Tokio
9//! - **Flexible** subscription management
10//! - **Thread-safe** by default
11//!
12//! ## Quick Example
13//!
14//! ```rust,no_run
15//! use tokio_events::{Event, EventBus};
16//!
17//! #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
18//! struct UserRegistered {
19//!     user_id: u64,
20//!     email: String,
21//! }
22//!
23//! impl Event for UserRegistered {
24//!     fn event_type() -> &'static str {
25//!         "UserRegistered"
26//!     }
27//! }
28//!
29//! #[tokio::main]
30//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
31//!     // Create event bus
32//!     let bus = EventBus::builder().build().await?;
33//!
34//!     // Subscribe to events
35//!     let handle = bus.subscribe(|event: UserRegistered| async move {
36//!         println!("New user registered: {}", event.email);
37//!     }).await?;
38//!
39//!     // Publish events
40//!     bus.publish(UserRegistered {
41//!         user_id: 123,
42//!         email: "user@example.com".to_string(),
43//!     }).await?;
44//!
45//!     // Unsubscribe when done
46//!     bus.unsubscribe(handle).await?;
47//!
48//!     Ok(())
49//! }
50//! ```
51
52#![warn(
53    missing_docs,
54    rust_2018_idioms,
55    missing_debug_implementations,
56    unreachable_pub
57)]
58#![cfg_attr(docsrs, feature(doc_cfg))]
59
60/// Core event system traits and types
61pub mod event;
62
63/// Error types and result aliases
64pub mod error;
65
66/// Event registry for type-to-subscriber mapping
67pub mod registry;
68
69/// Subscription management for event handlers
70pub mod subscription;
71
72/// Event dispatcher for routing events
73pub mod dispatcher;
74
75/// The main event bus implementation
76pub mod bus;
77pub mod global;
78
79/// Persistent storage for events
80#[cfg(feature = "persistence")]
81#[cfg_attr(docsrs, doc(cfg(feature = "persistence")))]
82pub mod persistence;
83
84// Re-export commonly used types
85pub use bus::{EventBus, EventBusBuilder};
86pub use error::{Error, Result};
87pub use event::{Event, EventEnvelope, EventMetadata, EventPriority, HasPriority};
88pub use subscription::{EventHandler, SubscriptionHandle};
89
90#[cfg(feature = "macros")]
91#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
92pub use tokio_events_macros::Event;
93
94/// Prelude module for convenient imports
95///
96/// # Example
97/// ```rust
98/// use tokio_events::prelude::*;
99/// ```
100pub mod prelude {
101    pub use crate::bus::{EventBus, EventBusBuilder};
102    pub use crate::error::{Error, Result};
103    pub use crate::event::{Event, EventPriority, HasPriority};
104    pub use crate::subscription::SubscriptionHandle;
105
106    #[cfg(feature = "macros")]
107    pub use tokio_events_macros::Event;
108}