futures_concurrency/future/join/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
use core::future::Future;
pub(crate) mod array;
pub(crate) mod tuple;
#[cfg(feature = "alloc")]
pub(crate) mod vec;
/// Wait for all futures to complete.
///
/// Awaits multiple futures simultaneously, returning the output of the futures
/// in the same container type they were created once all complete.
pub trait Join {
/// The resulting output type.
type Output;
/// The [`Future`] implementation returned by this method.
type Future: Future<Output = Self::Output>;
/// Waits for multiple futures to complete.
///
/// Awaits multiple futures simultaneously, returning the output of the futures
/// in the same container type they we're created once all complete.
///
/// # Examples
///
/// Awaiting multiple futures of the same type can be done using either a vector
/// or an array.
/// ```rust
/// # futures::executor::block_on(async {
/// use futures_concurrency::prelude::*;
///
/// // all futures passed here are of the same type
/// let fut1 = core::future::ready(1);
/// let fut2 = core::future::ready(2);
/// let fut3 = core::future::ready(3);
///
/// let outputs = [fut1, fut2, fut3].join().await;
/// assert_eq!(outputs, [1, 2, 3]);
/// # })
/// ```
///
/// In practice however, it's common to want to await multiple futures of
/// different types. For example if you have two different `async {}` blocks,
/// you want to `.await`. To do that, you can call `.join` on tuples of futures.
/// ```rust
/// # futures::executor::block_on(async {
/// use futures_concurrency::prelude::*;
///
/// async fn some_async_fn() -> usize { 3 }
///
/// // the futures passed here are of different types
/// let fut1 = core::future::ready(1);
/// let fut2 = async { 2 };
/// let fut3 = some_async_fn();
/// // ^ NOTE: no `.await` here!
///
/// let outputs = (fut1, fut2, fut3).join().await;
/// assert_eq!(outputs, (1, 2, 3));
/// # })
/// ```
///
/// <br><br>
/// This function returns a new future which polls all futures concurrently.
fn join(self) -> Self::Future;
}