Macro zng::task::any

source ·
macro_rules! any {
    ($fut0:expr $(,)?) => { ... };
    ($fut0:expr, $fut1:expr $(,)?) => { ... };
    ($fut0:expr, $fut1:expr, $fut2:expr $(,)?) => { ... };
    ($fut0:expr, $fut1:expr, $fut2:expr, $fut3:expr $(,)?) => { ... };
    ($fut0:expr, $fut1:expr, $fut2:expr, $fut3:expr, $fut4:expr $(,)?) => { ... };
    ($fut0:expr, $fut1:expr, $fut2:expr, $fut3:expr, $fut4:expr, $fut5:expr $(,)?) => { ... };
    ($fut0:expr, $fut1:expr, $fut2:expr, $fut3:expr, $fut4:expr, $fut5:expr, $fut6:expr $(,)?) => { ... };
    ($fut0:expr, $fut1:expr, $fut2:expr, $fut3:expr, $fut4:expr, $fut5:expr, $fut6:expr, $fut7:expr $(,)?) => { ... };
    ($($fut:expr),+ $(,)?) => { ... };
}
Expand description

A future that awaits for the first future that is ready.

The macro input is comma separated list of future expressions, the futures must all have the same output type. The macro output is a future that when “.awaited” produces a single output type instance returned by the first input future that completes.

At least one input future is required and any number of futures is accepted. For more than eight futures a proc-macro is used which may cause code auto-complete to stop working in some IDEs.

If two futures are ready at the same time the result of the first future in the input list is used. After one future is ready the other futures are not polled again and are dropped.

§Examples

Await for the first of three futures to complete:

use zng_task as task;
use zng_unit::*;

let r = task::any!(
    task::run(async { task::deadline(300.ms()).await; 'a' }),
    task::wait(|| 'b'),
    async { task::deadline(300.ms()).await; 'c' }
).await;

assert_eq!('b', r);