Module events

Source
Expand description

Different types of events.

Events should be used to coordinate between spawned tasks so that they can run in an event-driven manner and yield until there is something ready to process.

Basic events are created to be triggered once using notify() method. Any number of other tasks can be waiting for the event to be triggered. The listen() method is used to wait for the event to be triggered.

§Example:

An event being created to co-ordinate between two tasks.

fn spawn_listen<T>(engine: &mut Engine, event: Once<T>)
where
    T: Copy + 'static,
{
    engine.spawn(async move {
        event.listen().await;
        println!("After event");
        Ok(())
    });
}

fn spawn_notify<T>(engine: &mut Engine, event: Once<T>)
where
    T: Copy + 'static,
{
    let clock = engine.default_clock();
    engine.spawn(async move {
        clock.wait_ticks(10).await;
        println!("Trigger event");
        event.notify();
        Ok(())
    });
}

fn main() {
    let mut engine = Engine::default();
    let event = Once::default();
    spawn_listen(&mut engine, event.clone());
    spawn_notify(&mut engine, event);
    run_simulation!(engine);
}

Modules§

all_of
An event that is triggered when all events in the provided set are triggered.
any_of
An event that is triggered when one event in the provided set is triggered.
once
An event that can only be triggered once
repeated
An event that can be triggered multiple times. The event allows the notifier to pass a custom result to its listeners on each notification, using the notify_result() method. Alternatively, the last set result will be provided to the listeners. If no result has been set, the default value for the result type will be used.