-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides a `select!` macro for concurrently waiting on multiple async expressions. The macro has similar goals and syntax as the one provided by the `futures` crate, but differs significantly in implementation. First, this implementation does not require special traits to be implemented on futures or streams (i.e., no `FuseFuture`). A design goal is to be able to pass a "plain" async fn result into the select! macro. Even without `FuseFuture`, this `select!` implementation is able to handle all cases the `futures::select!` macro can handle. It does this by supporting pre-poll conditions on branches and result pattern matching. For pre-conditions, each branch is able to include a condition that disables the branch if it evaluates to false. This allows the user to guard futures that have already been polled, preventing double polling. Pattern matching can be used to disable streams that complete. A second big difference is the macro is implemented almost entirely as a declarative macro. The biggest advantage to using this strategy is that the user will not need to alter the rustc recursion limit except in the most extreme cases. The resulting future also tends to be smaller in many cases.
- Loading branch information
1 parent
f9ea576
commit 8cf98d6
Showing
14 changed files
with
1,468 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#![cfg(feature = "macros")] | ||
|
||
use futures::channel::oneshot; | ||
use futures::executor::block_on; | ||
use std::thread; | ||
|
||
#[test] | ||
fn join_with_select() { | ||
block_on(async { | ||
let (tx1, mut rx1) = oneshot::channel::<i32>(); | ||
let (tx2, mut rx2) = oneshot::channel::<i32>(); | ||
|
||
thread::spawn(move || { | ||
tx1.send(123).unwrap(); | ||
tx2.send(456).unwrap(); | ||
}); | ||
|
||
let mut a = None; | ||
let mut b = None; | ||
|
||
while a.is_none() || b.is_none() { | ||
tokio::select! { | ||
v1 = (&mut rx1), if a.is_none() => a = Some(v1.unwrap()), | ||
v2 = (&mut rx2), if b.is_none() => b = Some(v2.unwrap()), | ||
} | ||
} | ||
|
||
let (a, b) = (a.unwrap(), b.unwrap()); | ||
|
||
assert_eq!(a, 123); | ||
assert_eq!(b, 456); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use proc_macro::{TokenStream, TokenTree}; | ||
use proc_macro2::Span; | ||
use quote::quote; | ||
use syn::Ident; | ||
|
||
pub(crate) fn declare_output_enum(input: TokenStream) -> TokenStream { | ||
// passed in is: `(_ _ _)` with one `_` per branch | ||
let branches = match input.into_iter().next() { | ||
Some(TokenTree::Group(group)) => group.stream().into_iter().count(), | ||
_ => panic!("unexpected macro input"), | ||
}; | ||
|
||
let variants = (0..branches) | ||
.map(|num| Ident::new(&format!("_{}", num), Span::call_site())) | ||
.collect::<Vec<_>>(); | ||
|
||
// Use a bitfield to track which futures completed | ||
let mask = Ident::new( | ||
if branches <= 8 { | ||
"u8" | ||
} else if branches <= 16 { | ||
"u16" | ||
} else if branches <= 32 { | ||
"u32" | ||
} else if branches <= 64 { | ||
"u64" | ||
} else { | ||
panic!("up to 64 branches supported"); | ||
}, | ||
Span::call_site(), | ||
); | ||
|
||
TokenStream::from(quote! { | ||
pub(super) enum Out<#( #variants ),*> { | ||
#( #variants(#variants), )* | ||
// Include a `Disabled` variant signifying that all select branches | ||
// failed to resolve. | ||
Disabled, | ||
} | ||
|
||
pub(super) type Mask = #mask; | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.