pub struct FastSelect { /* private fields */ }
Expand description
An object that can be used to efficiently select over alternative futures.
This allocates storage used by calls to select
. Be careful
to preallocate any instances of this outside the hot path.
§Example
let mut fast_select = FastSelect::new();
let (_cancel_send, mut cancel_recv) = unbounded::<()>();
loop {
let operation = async {
Some(5)
};
let cancelled = async {
let _ = cancel_recv.next().await;
None
};
if let Some(value) = fast_select.select((operation, cancelled)).await {
break value;
}
}
In cases where one future is much more common than the others, you can leave that future out and use a traditional select macro or function to select between the common future and the tuple with the remaining futures. This may even be a tuple of length one. In this case, the common future will be polled every iteration, while the uncommon futures will be only polled as necessary.
For example:
let mut fast_select = FastSelect::new();
futures::select_biased! {
value = async { 5u32 }.fuse() => {
println!("{}", value);
}
_ = fast_select.select((pending::<u32>(),)).fuse() => {
unreachable!()
}
}
Implementations§
Source§impl FastSelect
impl FastSelect
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new FastSelect
.
Sourcepub async fn select<T: Select>(&mut self, futures: T) -> T::Output
pub async fn select<T: Select>(&mut self, futures: T) -> T::Output
Selects between the futures in tuple futures
.
Returns the output of the first one that completes. All the other futures are dropped without being completed.
The futures are polled in the order they are specified in the tuple, so there is a bias for earlier ones in the tuple.