crier
crier is a simple but flexible observer library for Rust.
Goals
Simplicity
crier has a simple API using basic Rust types. The complex types needed to make it work are all abstracted away from the consumer.
Flexibility
- A
Publishercan handle any number of different types of events and handlers. - You have several options when creating handlers:
- wrap a simple closure in the
Handlerstruct - implement the
Handletrait on your own type so that you have access to its other methods and state from thehandlemethod - implement the
HandleMuttrait on your own type so that you have mutable access to its other methods and states from thehandlemethod - mix and match all of the above
- wrap a simple closure in the
Usage
Subscribe a simple closure
use ;
;
;
Subscribe a custom type with a mut handler function
use ;
;
Check out the examples directory for more!
TODO:
- Publisher supports any number of handlers / events of different types
- Derive macro for
Eventtrait - Optional async feature using Tokio
- Support handlers that take a
mut &selfreceiver when implementingHandlefor custom types to enable more complex use cases, e.g. updating some internal state when events are received
Acknowledgements
Thanks to @klaatu01 for giving me the idea to build this and the coaching I needed to wrangle Rust's type system into allowing events and handlers of multiple different types in a single publisher.