tiny-pubsub
In-process, topic-based publish/subscribe message bus for Rust. Hierarchical topics, deferred or synchronous delivery, no dependencies.
Install
[]
= "0.1"
Use
use PubSub;
use Cell;
use Rc;
let bus: = new;
let hits = new;
let h = hits.clone;
// Subscribe a callback. You get back a token for later removal.
let token = bus.subscribe;
// Synchronous publish runs every matching subscriber before it returns.
assert!;
assert_eq!;
bus.unsubscribe;
Topics are hierarchical
Topics are dot-delimited. Publishing a leaf notifies the leaf and every ancestor
prefix, then the wildcard *. Children and siblings are never notified.
Publishing a.b.c reaches subscribers of a.b.c, then a.b, then a, then
*. An ancestor subscriber receives the original leaf topic as its first
argument, not the ancestor it matched.
use PubSub;
let bus: = new;
bus.subscribe;
bus.subscribe;
assert!;
Deferred delivery
publish queues delivery and returns at once. Subscribers run when you call
process_deferred. This models a non-blocking publisher.
use PubSub;
use Cell;
use Rc;
let bus: = new;
let fired = new;
let f = fired.clone;
bus.subscribe;
bus.publish;
assert!; // nothing has run yet
bus.process_deferred; // drain the queue
assert!;
Both publish and publish_sync return true when the topic had at least one
matching subscriber (direct, ancestor, or *), computed before any subscriber
runs.
Removing subscriptions
unsubscribe(&token)removes one subscription. It returns the token on a match,Noneotherwise.unsubscribe_topic(topic)removes a topic and every descendant, by string prefix. It returnstruewhen the prefix matched a topic.clear_subscriptions(prefix)andclear_all_subscriptions()clear in bulk.
Rust closures cannot be compared by identity, so removal by function value has no analogue. The token covers single-subscription removal.
Panics during delivery
By default a panicking subscriber does not block the others. Delivery finishes,
then the first panic is re-raised. Call set_immediate_exceptions(true) to stop
at the first panic and propagate it, skipping the rest.
License
MIT.