eventbus-tiny 0.1.0

A small, dependency-free crate that provides a multi-producer broadcast-consumer event bus
Documentation
  • Coverage
  • 100%
    6 out of 6 items documented1 out of 1 items with examples
  • Size
  • Source code size: 25.82 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.96 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • KizzyCode/eventbus-tiny-rust
    3 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • KizzyCode

License BSD-2-Clause License MIT AppVeyor CI docs.rs crates.io Download numbers dependency status

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());