Module any_of

Source
Expand description

An event that is triggered when one event in the provided set is triggered.

The AnyOf will also return the data associated with the event that fired.

§Example

Here is a basic example of creating a custom enum and using it to handle different events in different ways.

// Create the `enum` that defines all values returned by the event. It must
// implement `Clone` and `Copy`
#[derive(Clone, Copy)]
enum EventResult {
    TimedOut,
    AllOk,
    // ... other results
}
// Create the events
let timeout = Once::new(EventResult::TimedOut);
let ok = Once::new(EventResult::AllOk);
let anyof = AnyOf::new(vec![Box::new(timeout.clone()), Box::new(ok.clone())]);

// Spawn a task that will trigger the timeout
engine.spawn(async move {
    clock.wait_ticks(10000).await;
    timeout.notify();
    Ok(())
});

// Spawn a task that will say all is ok
engine.spawn(async move {
    clock.wait_ticks(1000).await;
    ok.notify();
    Ok(())
});

// Handle the events
engine.spawn(async move {
    match anyof.listen().await {
        EventResult::TimedOut => panic!("Timed out"),
        EventResult::AllOk => println!("All Ok"),
        // ...
    }
    Ok(())
});

Structs§

AnyOf
AnyOfFuture
AnyOfState