[][src]Crate revent

Mutable aliasing free event system for Rust.

What is an event system?

An event system is a system where an object can create an event, and any other object (including the one generating the event itself) can listen and react to this event and update its state.

Why do we want this?

It allows us to decouple objects that need to uphold some invariant with respect to each other.

Take for instance a video player. If it loads a video then it should probably update the run-time of that video in the GUI. So the video loader can emit an event of the type VideoLoaded which the GUI listens to and updates its state.

The alternative is to somehow encode this ad-hoc, by calling an update function for the GUI inside the video loader. This becomes unwieldy in large programs.

Example of basic usage

use revent::{Event, EventStore, Notifiable};
use std::any::TypeId;

struct MyEvent;

impl Event for MyEvent { }

struct MyNotifiable;

impl Notifiable for MyNotifiable {
    fn notify(&mut self, event: &dyn Event, _: &mut EventStore) {
        println!("I was notified!");
        if event.type_id() == TypeId::of::<MyEvent>() {
            println!("This was MyEvent!");
            // Do something useful...
        }
    }
}

let mut mn = MyNotifiable { };

mn.with_notify(|this, store| {
    store.emit(MyEvent { });
});

The order in which events are processed is FIFO (first-in, first-out). Meaning that emitting an event guarantees that events emitted before will be run before.

Structs

EventStore

Event storage. Events are EventStore::emitted into this structure.

Traits

Event

A generic event.

Notifiable

Main trait of this crate to implement on structures.