9 breaking releases

0.12.2 Sep 14, 2023
0.12.0 Jul 22, 2023
0.11.0 Jun 28, 2023

#186 in Concurrency

34 downloads per month
Used in 4 crates (via logid-core)

MIT license

65KB
974 lines

evident

build-test mantra-sync crates.io

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 events
  • EventEntry ... Allows adding additional information to an event
  • IntermediaryEvent ... Allows automatic capturing of events once they go out of scope

Optional traits to further customize evident:

  • Filter ... To prevent capturing events
  • Msg ... Allows creating a custom message to be sent with an event

Creating your pub/sub instance:

Examples:

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

~570KB