Skip to content

Commit 65b9e62

Browse files
Merge #243
243: future docs r=stjepang a=yoshuawuyts Docs for futures concurrency. We currently don't have anything, and I figured this would be helpful in pointing folks in the right direction. Thanks! ## Screenshot ![Screenshot_2019-09-27 async_std future - Rust](https://user-images.githubusercontent.com/2467194/65730755-3c836780-e0c3-11e9-9bc5-fbf400cec7b2.png) Co-authored-by: Yoshua Wuyts <[email protected]>
2 parents 184427c + 70069e0 commit 65b9e62

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/future/mod.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
//! Asynchronous values.
2+
//!
3+
//! ## Base Futures Concurrency
4+
//!
5+
//! Often it's desireable to await multiple futures as if it was a single
6+
//! future. The `join` family of operations converts multiple futures into a
7+
//! single future that returns all of their outputs. The `select` family of
8+
//! operations converts multiple future into a single future that returns the
9+
//! first output.
10+
//!
11+
//! For operating on futures the following macros can be used:
12+
//!
13+
//! | Name | Return signature | When does it return? |
14+
//! | --- | --- | --- |
15+
//! | `future::join` | `(T1, T2)` | Wait for all to complete
16+
//! | `future::select` | `T` | Return on first value
17+
//!
18+
//! ## Fallible Futures Concurrency
19+
//!
20+
//! For operating on futures that return `Result` additional `try_` variants of
21+
//! the macros mentioned before can be used. These macros are aware of `Result`,
22+
//! and will behave slightly differently from their base variants.
23+
//!
24+
//! In the case of `try_join`, if any of the futures returns `Err` all
25+
//! futures are dropped and an error is returned. This is referred to as
26+
//! "short-circuiting".
27+
//!
28+
//! In the case of `try_select`, instead of returning the first future that
29+
//! completes it returns the first future that _successfully_ completes. This
30+
//! means `try_select` will keep going until any one of the futures returns
31+
//! `Ok`, or _all_ futures have returned `Err`.
32+
//!
33+
//! However sometimes it can be useful to use the base variants of the macros
34+
//! even on futures that return `Result`. Here is an overview of operations that
35+
//! work on `Result`, and their respective semantics:
36+
//!
37+
//! | Name | Return signature | When does it return? |
38+
//! | --- | --- | --- |
39+
//! | `future::join` | `(Result<T, E>, Result<T, E>)` | Wait for all to complete
40+
//! | `future::try_join` | `Result<(T1, T2), E>` | Return on first `Err`, wait for all to complete
41+
//! | `future::select` | `Result<T, E>` | Return on first value
42+
//! | `future::try_select` | `Result<T, E>` | Return on first `Ok`, reject on last Err
243
344
#[doc(inline)]
445
pub use std::future::Future;

0 commit comments

Comments
 (0)