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