9 breaking releases
0.12.2 | Sep 14, 2023 |
---|---|
0.12.0 | Jul 22, 2023 |
0.11.0 | Jun 28, 2023 |
#278 in Concurrency
Used in 3 crates
(via logid-core)
65KB
974 lines
evident
This crate makes it easy to create custom multithreaded pub/sub functionality for Rust applications. It uses events to send information between publisher and subscriber, and IDs are used to subscribe and identify these events (hence evIDent).
The pub/sub communication is done over a central static publisher that captures set events, and forwards them to subscribers. To allow using evident in different scenarios, a publisher must be created per scenario, and cannot be provided by evident itself. See the setup section below on how to create your custom pub/sub instance.
Setup
To customize evident to fit your use case, you need to implement the following traits:
Id
... Defines the structure of the ID that is used to identify eventsEventEntry
... Allows adding additional information to an eventIntermediaryEvent
... Allows automatic capturing of events once they go out of scope
Optional traits to further customize evident:
Filter
... To prevent capturing eventsMsg
... Allows creating a custom message to be sent with an event
Creating your pub/sub instance:
create_static_publisher!()
... Convenience macro to create your customEvidentPublisher
create_set_event_macro!()
... Convenience macro to create theset_event!()
macro that may be used to set your custom events
Examples:
- /tests/min_concretise ... Contains a minimal pub/sub setup
- /tests/min_filter ... Contains a minimal pub/sub setup using a custom filter
- /tests/min_msg ... Contains a minimal pub/sub setup with a custom message
Usage
After creating your own publisher, you can set events using the set_event!()
macro.
Example:
let some_id = MinId { id: 3 };
let msg = "Some msg";
let sub = PUBLISHER.subscribe(some_id).unwrap();
set_event!(some_id, msg).finalize();
let event = sub
.get_receiver()
.recv_timeout(std::time::Duration::from_millis(100))
.unwrap();
Note: finalize()
is set explicitly to ensure the event is sent before the subscription tries to receive it.
Otherwise, it would be sent once the event gets out of scope (is dropped).
License
MIT Licensed
Dependencies
~585KB