/tuple_fut

join and select as methods on tuples instead of macros

Primary LanguageRustMIT LicenseMIT

tuple-fut

Apache-2.0 licensed MIT licensed crates.io Released API docs

join and select as methods on tuples instead of macros.

Select

Call select() on a tuple of futures to await the first one that completes. They must all have the same output type.

Example

use tuple_fut::Select;

let result = (fut1, fut2, fut3).select().await;

Join

Call join() on a tuple of futures to await all of them. It returns a tuple of the output values of the resolved futures.

Example

use tuple_fut::Join;

let (res1, res2, res3) = (fut1, fut2, fut3).join().await;

Caveats.

All futures must be Unpin. That means you cannot use an async function directly as future in a tuple. You need to pin it first, for example by using tokio::pin or futures::pin_mut.

Example

use tuple_fut::Select;

async fn foo() -> u32 {
    42
}

async fn bar() -> u32 {
    23
}

async fn something() -> u32 {
    tokio::pin {
        let foo = foo();
        let bar = bar();
    }

    (foo, bar).select().await
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.