futures_util/async_await/
stream_select_mod.rs

1//! The `stream_select` macro.
2
3#[allow(unreachable_pub)]
4#[doc(hidden)]
5pub use futures_macro::stream_select_internal;
6
7#[allow(clippy::too_long_first_doc_paragraph)]
8/// Combines several streams, all producing the same `Item` type, into one stream.
9/// This is similar to `select_all` but does not require the streams to all be the same type.
10/// It also keeps the streams inline, and does not require `Box<dyn Stream>`s to be allocated.
11/// Streams passed to this macro must be `Unpin`.
12///
13/// If multiple streams are ready, one will be pseudo randomly selected at runtime.
14///
15/// # Examples
16///
17/// ```
18/// # futures::executor::block_on(async {
19/// use futures::{stream, StreamExt, stream_select};
20/// let endless_ints = |i| stream::iter(vec![i].into_iter().cycle()).fuse();
21///
22/// let mut endless_numbers = stream_select!(endless_ints(1i32), endless_ints(2), endless_ints(3));
23/// match endless_numbers.next().await {
24///     Some(1) => println!("Got a 1"),
25///     Some(2) => println!("Got a 2"),
26///     Some(3) => println!("Got a 3"),
27///     _ => unreachable!(),
28/// }
29/// # });
30/// ```
31#[macro_export]
32macro_rules! stream_select {
33    ($($tokens:tt)*) => {{
34        use $crate::__private as __futures_crate;
35        $crate::stream_select_internal! {
36            $( $tokens )*
37        }
38    }}
39}