Macro zng::task::all_ok

source ·
macro_rules! all_ok {
    ($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 is ready when all futures are ready with an Ok(T) result or any future is ready with an Err(E) result.

The output type is Result<(T0, T1, ..), E>, the Ok type is a tuple with all the Ok values, the error type is the first error encountered, the input futures must have the same Err type but can have different Ok types.

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 and Err(E) at the same time the result of the first future in the input list is used. After one future is ready and Err(T) the other futures are not polled again and are dropped. After a future is ready it is also not polled again and dropped.

§Examples

Await for the first of three futures to complete with Ok(T):

use zng_task as task;
let r = task::all_ok!(
    task::run(async { Ok::<_, FooError>('a') }),
    task::wait(|| Ok::<_, FooError>('b')),
    async { Ok::<_, FooError>('c') }
).await;

assert_eq!(Ok(('a', 'b', 'c')), r);

And in if any completes with Err(E):

use zng_task as task;
let r = task::all_ok!(
    task::run(async { Ok('a') }),
    task::wait(|| Err::<char, _>(FooError)),
    async { Ok('c') }
).await;

assert_eq!(Err(FooError), r);