macro_rules! any_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 waits for the first future that is ready with an Ok(T)
result.
The macro input is comma separated list of future expressions, the futures must
all have the same output Result<T, E>
type, but each can have a different E
. The macro output is a future
that when “.awaited” produces a single output of type Result<T, (E0, E1, ..)>
that is Ok(T)
if any of the futures
is Ok(T)
or is Err((E0, E1, ..))
is all futures are Err
.
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 Ok(T)
at the same time the result of the first future in the input list is used.
After one future is ready and Ok(T)
the other futures are not polled again and are dropped. After a future
is ready and Err(E)
it is also not polled again and dropped.
§Examples
Await for the first of three futures to complete with Ok
:
use zng_task as task;
let r = task::any_ok!(
task::run(async { Err::<char, _>("error") }),
task::wait(|| Ok::<_, FooError>('b')),
async { Err::<char, _>(FooError) }
).await;
assert_eq!(Ok('b'), r);