Skip to content

Commit 2252059

Browse files
committed
init Future::select
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
1 parent da795de commit 2252059

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

src/future/future.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,40 @@ extension_trait! {
129129
{
130130
DelayFuture::new(self, dur)
131131
}
132+
133+
#[doc = r#"
134+
Waits for either one of several similarly-typed futures to complete.
135+
136+
Awaits multiple futures simultaneously, returning all results once complete.
137+
138+
This function will return a new future which awaits for either one of both
139+
futures to complete. If multiple futures are completed at the same time,
140+
resolution will occur in the order that they have been passed.
141+
142+
Note that this macro consumes all futures passed, and once a future is
143+
completed, all other futures are dropped.
144+
145+
This macro is only usable inside of async functions, closures, and blocks.
146+
147+
# Examples
148+
149+
```
150+
#![feature(async_await)]
151+
# futures::executor::block_on(async {
152+
use futures::future;
153+
154+
let a = future::pending();
155+
let b = future::ready(1u8);
156+
let c = future::ready(2u8);
157+
158+
let f = a.select(b).select(c);
159+
assert_eq!(f.await, 1u8);
160+
# });
161+
```
162+
"#]
163+
fn select(&mut self) -> () {
164+
()
165+
}
132166
}
133167

134168
impl<F: Future + Unpin + ?Sized> Future for Box<F> {

src/future/mod.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
//!
1111
//! For operating on futures the following macros can be used:
1212
//!
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
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
1717
//!
1818
//! ## Fallible Futures Concurrency
1919
//!
@@ -34,12 +34,17 @@
3434
//! even on futures that return `Result`. Here is an overview of operations that
3535
//! work on `Result`, and their respective semantics:
3636
//!
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
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
43+
//!
44+
//! [`future::join!`]: macro.join.html
45+
//! [`future::try_join!`]: macro.try_join.html
46+
//! [`Future::select`]: trait.Future.html#method.select
47+
//! [`Future::try_select`]: trait.Future.html#method.try_select
4348
4449
#[doc(inline)]
4550
pub use async_macros::{join, try_join};
@@ -57,9 +62,6 @@ mod ready;
5762
mod timeout;
5863

5964
cfg_unstable! {
60-
#[doc(inline)]
61-
pub use async_macros::{select, try_select};
62-
6365
pub use into_future::IntoFuture;
6466
mod into_future;
6567
}

0 commit comments

Comments
 (0)