[][src]Crate revent

Event broker library for Rust.

Implements a synchronous event broker that does not violate mutability constraints. It does so by performing DAG traversals at initialization-time to ensure that no signal chains are able to form a loop.

Because of this it can forgo mutable aliasing checks, such that emitting events is as fast and cheap as calling a list of trait objects.

What is an event broker?

An event broker is a bag of objects and a bunch of "signals". Each object decides which signals to listen to.

Each object (also called subscriber) is notified when its subscribed signal is emitted. A subscriber may during notification processing emit yet another signal into the broker, and so on.

use revent::{hub, Subscriber};

// Construct a trait for some type of event.
pub trait MyEventHandler {
    fn my_event(&mut self);
}

// Create an event hub.
// This event hub is the "top level" event hub, it is owned by the calling code and must list
// all events that this system will have. It is a comma-separaetd list of the form `name: type`.
hub! {
    Hub {
        event: dyn MyEventHandler,
    }
}

// Create a derivative event hub.
// This event hub specifies which of the "top level" event hub signals it wishes to signal
// and subscribe to.
hub! {
    // Name of the new derivative hub.
    XHub: Hub {
    //    ^^^ - Hub to base itself of, these can also be other derivative event hubs. Is a
    // list of comma-separated hubs.
        // Here comes a list of signals we want to notify, currently there are none.
    } subscribe X {
    //          ^ - Structure to bind to this derivative hub.
        event,
    //  ^^^^^ - We make X subscribe to the `event` channel.
    }
}

// Construct a top-level hub object.
let mut hub = Hub::default();

// Implement a subscriber to some event.
struct X;
impl MyEventHandler for X {
    fn my_event(&mut self) {
        println!("Hello world");
    }
}
impl Subscriber for X {
    // Connect X to the XHub created by the macro.
    type Hub = XHub;
    type Input = ();
    fn build(_: Self::Hub, input: Self::Input) -> Self {
        Self
    }
}

// Create an instance of X in the hub.
hub.subscribe::<X>(());

// Now emit an event into the topic.
hub.event.emit(|x| {
    x.my_event();
});

See the examples directory for more.

Logging

Use feature = "slog" to add a method log to the hub generated from the hub macro. This method sets a logger object.

Modules

example

Contains an example hub implementation generated by the hub macro.

Macros

hub

Generate an event hub or a derivative and its associated boilerplate code.

Structs

Topic

An event channel for a certain type of Subscriber.

Traits

Subscriber

Subscriber to an event hub.