Macro allochronic_util::select [−][src]
select!() { /* proc-macro */ }
Polls all passed Future
s or
Stream
s until one is ready and returns
it. Prioritizes by order.
Raw-mode
If the complete
item is unspecified, Future
s and
Stream
s will be pulled beyond exhaustion. If a high
priority item is exhausted and select!
is being used in
a loop, it will continuously return the same result, which might be
unintended.
Fused-mode
If the complete
item is specified, all polled items have to implement
FusedFuture
or FusedStream
, for Stream
s this
will unwrap the Option
. The complete
item will be executed once all
items have been exhausted. Items will not be pulled beyond exhaustion.
Examples
Raw-mode:
use futures_util::stream; use allochronic_util::select; let mut stream1 = stream::iter(1..=3); let mut stream2 = stream::iter(4..=6); let mut stream3 = stream::iter(7..=9); let mut counter = 0; while let Some(result) = select![ result: &mut stream1 => result, result: &mut stream2 => result, result: &mut stream3 => result, ] { counter += 1; assert_eq!(counter, result); } // this loop will only reach the number of three, because it will abort after the first stream // is exhausted assert_eq!(counter, 3);
Fused-mode:
use futures_util::stream::{self, StreamExt}; use allochronic_util::select; let mut stream1 = stream::iter(1..=3).fuse(); let mut stream2 = stream::iter(4..=6).fuse(); let mut stream3 = stream::iter(7..=9).fuse(); let mut counter = 0; while let Some(result) = select![ result: &mut stream1 => Some(result), result: &mut stream2 => Some(result), result: &mut stream3 => Some(result), complete => None, ] { counter += 1; assert_eq!(counter, result); } assert_eq!(counter, 9);