Expand description
§eventbus-tiny
Welcome to eventbus-tiny 🎉
eventbus-tiny is a small, dependency-free, no-unsafe crate that provides a multi-producer broadcast-consumer event
bus for arbitrary event types (as long as they are Send).
§Implementation and Locking
The implementation nearly lock-free, only requiring a write-lock if the registry changes (i.e. if a new subscriber is
added, or shrink_to_fit is called) - everything else uses either a cooperative read-lock, or is completely lockfree
via the underlying std::sync::mpsc-channels.
This approach provides a reasonable compromise between ease-of-implementation (no dependencies, no unsafe code,
well-known semantics of built-in types), and performance with lock-free operation in all critical hot-zones like event
publication and awaiting/receiving events.
§Example
use eventbus_tiny::EventBus;
use eventbus_tiny::Subscription;
// Create bus and subscribers
let bus = EventBus::new();
let subscriber_a: Subscription<usize> = bus.subscribe(64);
let subscriber_b: Subscription<usize> = bus.subscribe(64);
// Publish some events and ensure they get delivered to all two subscribers
assert_eq!(bus.publish(1usize), 2);
assert_eq!(bus.publish(4usize), 2);
assert_eq!(bus.publish(7usize), 2);
// Receive events
assert_eq!(subscriber_a.recv(), Ok(1usize));
assert_eq!(subscriber_a.recv(), Ok(4usize));
assert_eq!(subscriber_a.recv(), Ok(7usize));
assert_eq!(subscriber_b.recv(), Ok(1usize));
assert_eq!(subscriber_b.recv(), Ok(4usize));
assert_eq!(subscriber_b.recv(), Ok(7usize));
// Drop bus and assert the subscribers are dead
drop(bus);
assert!(subscriber_a.recv().is_err());
assert!(subscriber_b.recv().is_err());Macros§
- enum_
from - Implements
Fromto convert$sourceinto$enum::$variant($source) - where_
into - Creates a filter-map clause that maps any event of type
$sourceintoSome(T) - where_
try_ into - Creates a filter-map clause that tries to map any event of type
$sourceintoSome(T)
Structs§
- Event
Bus - A tiny pub/sub event bus
- Subscription
- An event subscription channel