| 1 | use async_fn_stream::fn_stream; |
| 2 | use futures_util::{pin_mut, Stream, StreamExt}; |
| 3 | |
| 4 | fn build_stream() -> impl Stream<Item = i32> { |
| 5 | fn_stream(|emitter| async move { |
| 6 | for i in 0..3 { |
| 7 | // yield elements from stream via `emitter` |
| 8 | emitter.emit(i).await; |
| 9 | } |
| 10 | }) |
| 11 | } |
| 12 | |
| 13 | async fn example() { |
| 14 | let stream = build_stream(); |
| 15 | |
| 16 | pin_mut!(stream); |
| 17 | let mut numbers = Vec::new(); |
| 18 | while let Some(number) = stream.next().await { |
| 19 | print!("{number} "); |
| 20 | numbers.push(number); |
| 21 | } |
| 22 | println!(); |
| 23 | assert_eq!(numbers, vec![0, 1, 2]); |
| 24 | } |
| 25 | |
| 26 | pub fn main() { |
| 27 | futures_executor::block_on(example()); |
| 28 | } |