race_ok

Function race_ok 

Source
pub fn race_ok<T: RaceOkTuple>(futures: T) -> RaceOk<T>
Expand description

Wait for the first future in a tuple to successfully complete.

Requires the std feature, as std::panic::catch_unwind is needed when polling the futures to ensure soundness.

ยงExamples

use completion::{future, completion_async};
use futures_lite::future::yield_now;

assert_eq!(
    future::race_ok((
        completion_async!(Err(())),
        completion_async!(Ok::<_, ()>(3)),
        std::future::pending::<Result<_, ()>>(),
    ))
    .await,
    Ok(3),
);

If all the futures fail, this will return a tuple of the errors.

use completion::{future, completion_async};
use futures_lite::future::yield_now;

assert_eq!(
    future::race_ok((
        completion_async!(Err::<(), _>(0)),
        completion_async!(Err(1)),
        completion_async!(Err(2)),
    ))
    .await,
    Err((0, 1, 2)),
);

If multiple futures are immediately successfully ready, the earlier one will be chosen. However after this polling will be fair.

use completion::{future, completion_async};
use futures_lite::future::yield_now;

assert_eq!(
    future::race_ok((
        completion_async! {
            yield_now().await;
            Ok::<_, ()>(0)
        },
        completion_async!(Ok::<_, ()>(1)),
        completion_async!(Err(2)),
        completion_async!(Ok::<_, ()>(3)),
    ))
    .await,
    Ok(1),
);