Skip to content

Commit db1a70d

Browse files
committed
try_select done
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent 20d24a2 commit db1a70d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

examples/main.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
fn main() {
2+
futures::executor::block_on(async {
3+
async fn main() -> Result<(), std::io::Error> {
4+
use async_macros::try_select;
5+
use futures::future;
6+
use std::io::{Error, ErrorKind};
7+
8+
let a = future::pending::<Result<u8, Error>>();
9+
let b = future::ready(Err(Error::from(ErrorKind::Other)));
10+
let c = future::ready(Ok(1u8));
11+
12+
assert_eq!(try_select!(a, b, c).await?, 1u8);
13+
Ok(())
14+
}
15+
main().await.unwrap();
16+
});
17+
}

src/try_select.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/// ```
3131
#[macro_export]
3232
macro_rules! try_select {
33-
($($fut:ident),* $(,)?) => { {
33+
($($fut:ident),+ $(,)?) => { {
3434
async {
3535
$(
3636
// Move future into a local so that it is pinned in one place and
@@ -52,12 +52,25 @@ macro_rules! try_select {
5252
let fut = unsafe { Pin::new_unchecked(&mut $fut) };
5353
let output = fut.take_output().unwrap();
5454
return Poll::Ready(output);
55+
} else {
56+
all_done = false;
5557
}
58+
} else {
59+
all_done = false;
5660
}
5761
)*
5862

5963
if all_done {
60-
unimplemented!();
64+
// We need to iterate over all items to not get an
65+
// "unreachable code" warning.
66+
let mut err = None;
67+
$(
68+
if err.is_none() {
69+
let fut = unsafe { Pin::new_unchecked(&mut $fut) };
70+
err = Some(fut.take_output().unwrap());
71+
}
72+
)*
73+
return Poll::Ready(err.unwrap());
6174
} else {
6275
Poll::Pending
6376
}

0 commit comments

Comments
 (0)