[][src]Macro pasts::race

macro_rules! race {
    ($f:expr) => { ... };
}

Create a future that waits on multiple futures concurrently and returns the first result.

Takes an array of types that implement Future and Unpin.

Example: Await on The Fastest Future

race!() will always poll the first future in the array first.

use core::{future::Future, pin::Pin};
use pasts::race;

async fn async_main() {
    let hello: Pin<Box<dyn Future<Output=&str>>> = Box::pin(async { "Hello" });
    let world: Pin<Box<dyn Future<Output=&str>>> = Box::pin(async { "World" });
    let mut array = [hello, world];
    // Hello is ready, so returns with index and result.
    assert_eq!((0, "Hello"), race!(array));
}

pasts::block_on(async_main());