1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! # toys
//!
//! Lightweight application primitives for async Rust, built on [Tokio].
//!
//! Each primitive lives behind a feature flag so you only compile what you need.
//!
//! ## Feature flags
//!
//! | Flag | Default | Enables |
//! |------|:-------:|---------|
//! | `event` | | Typed global broadcast event bus and the [`asynchronous`] attribute macro |
//!
//! ## Event system
//!
//! The event system decouples producers and consumers of typed events through a
//! process-wide broadcast channel. Any number of handlers can subscribe to the same
//! event type independently.
//!
//! ```text
//! event::<E>().dispatch(e)
//! │
//! ▼
//! ┌──────────────────────┐
//! │ EventChannel<E> │ ← one per type, process-global
//! └──────────┬───────────┘
//! │ broadcast
//! ┌──────────────┼──────────────┐
//! ▼ ▼ ▼
//! Handler A Handler B Handler C
//! ```
//!
//! ### Minimal example
//!
//! ```no_run
//! use toys::event::{event, EventHandler, EventLoop};
//! use toys::asynchronous;
//! use std::sync::Arc;
//!
//! pub enum Signal { Ping }
//!
//! struct Logger;
//!
//! #[asynchronous]
//! impl EventHandler<Signal> for Logger {
//! async fn handle(self: Arc<Self>, _event: Signal) {
//! println!("ping received");
//! }
//! }
//!
//! # tokio_test::block_on(async {
//! let logger = Arc::new(Logger) as Arc<dyn EventHandler<Signal>>;
//! EventLoop::<Signal>::new().dispatch(&[logger]).await;
//! event::<Signal>().dispatch(Signal::Ping).await.unwrap();
//! # });
//! ```
//!
//! [`EventHandler`]: event::EventHandler
//! [`EventLoop`]: event::EventLoop
//! [`EventLoop::dispatch`]: event::EventLoop::dispatch
//! [Tokio]: https://tokio.rs
/// Typed global broadcast event bus.
///
/// See [`event::event`] to obtain a channel, [`event::EventHandler`] to react to
/// events, and [`event::EventLoop`] to register handlers.
/// Transforms `async fn` methods in `trait` and `impl` blocks into boxed,
/// dyn-compatible futures.
///
/// Re-exported from `toys_macros`. See that crate's documentation for supported
/// arguments (`no_sync`, `local`, `static_lifetime`).
pub use asynchronous;