Crates.io | evident |
lib.rs | evident |
version | 0.12.2 |
source | src |
created_at | 2023-05-11 23:57:28.208199 |
updated_at | 2023-09-14 10:04:52.89555 |
description | Pub/Sub library using IDs to identify events. |
homepage | |
repository | https://github.com/mhatzl/evident |
max_upload_size | |
id | 862520 |
size | 133,121 |
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.
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 scopeOptional traits to further customize evident:
Filter
... To prevent capturing eventsMsg
... Allows creating a custom message to be sent with an eventCreating your pub/sub instance:
create_static_publisher!()
... Convenience macro to create your custom EvidentPublisher
create_set_event_macro!()
... Convenience macro to create the set_event!()
macro that may be used to set your custom eventsExamples:
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).
MIT Licensed