Deprecated
This crate is deprecated as of 2026-10. It will receive no new features. Existing versions remain on crates.io.
For a few handlers, a hand-rolled HashMap<K, Vec<Box<dyn Fn>>> is straightforward. For a fuller API, search crates.io for established event-emitter crates.
keyed-emitter
String-keyed event emitter with typed payloads and synchronous dispatch.
Register closures under an event name, each for one payload type. emit borrows
a payload, runs every listener on that event whose type matches, and returns how
many ran. Listeners for another type are skipped and keep their call budget.
Dispatch happens on the caller's thread, and the crate has no dependencies.
use EventEmitter;
use ;
let seen = new;
let sink = clone;
let mut emitter = new;
emitter.;
let id = emitter.;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
Dispatch rules
- Every callback runs on the caller's thread before
emitreturns, in registration order. - A listener runs only when its registered
Tequals the emittedT. A mismatched listener is skipped and keeps its budget. One event name can carry listeners for several types. - The return value is the number of callbacks invoked. An unknown event, an
empty event, or an all-mismatched event returns
0. Tneeds'staticand nothing else. Payloads that are notClone, notSend, and not serializable work, because the listener gets&T.onceandon_limited(n)listeners are charged only when they run, and a listener whose budget hits zero is removed within that sameemit.on_limited(_, 0, _)stores nothing.remove_listenerreturnstrueonce per live listener andfalsefor an unknown, removed, or exhausted id.listener_countreports live listeners on an event across all types.ListenerIdis unique within one emitter for its lifetime. Two emitters can mint the same id.
Threads
EventEmitter is Send and not Sync. Registration and dispatch take
&mut self. To share one across threads, wrap it in a Mutex. A
static OnceLock<Mutex<_>> gives a process-wide emitter. See the crate docs
for the full example.
Locking that Mutex again from inside a callback is a self-deadlock. Record
what you need in the callback and act on it after emit returns.
Panics
No method panics on any input. Allocation failure, a panic raised by a user
callback, or a panic raised by a callback capture's destructor during listener
pruning can cross emit. A callback panic unwinds out of emit with the
emitter coherent. The budget is charged before the callback runs, so a once
listener that panicked does not fire again. Exhausted entries are pruned before
that panic is re-raised. The test suite drives this case in debug and release.
Cost
A single emit over n listeners is O(n), including the prune of exhausted
listeners. benches/emit_linearity.rs and the emit_linearity test measure
interleaved once and on listeners at 20k, 40k, and 80k and fail when the
time per doubling exceeds 3x. remove_listener is a linear scan over all
events.
Toolchain
Rust 1.70 or newer. Zero dependencies. #![forbid(unsafe_code)]. CI runs the
suite on stable, 1.70.0, and beta, and runs the no-panic suite under
panic = "abort" on nightly.
License
MIT.