Skip to content

mhatzl/evident

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Custom pub/sub for Rust applications.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published