File: numbers.rs

package info (click to toggle)
rust-async-fn-stream 0.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 116 kB
  • sloc: makefile: 2; sh: 1
file content (28 lines) | stat: -rw-r--r-- 651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use async_fn_stream::fn_stream;
use futures_util::{pin_mut, Stream, StreamExt};

fn build_stream() -> impl Stream<Item = i32> {
    fn_stream(|emitter| async move {
        for i in 0..3 {
            // yield elements from stream via `collector`
            emitter.emit(i).await;
        }
    })
}

async fn example() {
    let stream = build_stream();

    pin_mut!(stream);
    let mut numbers = Vec::new();
    while let Some(number) = stream.next().await {
        print!("{} ", number);
        numbers.push(number);
    }
    println!();
    assert_eq!(numbers, vec![0, 1, 2]);
}

pub fn main() {
    futures_executor::block_on(example());
}