Skip to main content

async_stream

Function async_stream 

Source
pub fn async_stream<T, F: Future<Output = ()>>(
    generator: impl FnOnce(Emitter<T>) -> F,
) -> impl FusedStream<Item = T>
Expand description

Creates a Stream from an async generator function.

The generator closure receives an Emitter<T> and runs as an async block. Each emitter.emit(value).await call suspends the generator and produces the next item in the stream. The stream ends when the generator future resolves.

ยงExample

use datafusion_execution::async_stream;
use futures::StreamExt;

let stream = async_stream(|mut emitter| async move {
    for i in 0_i32..3 {
        emitter.emit(i).await;
    }
});

let values: Vec<i32> = stream.collect().await;
assert_eq!(values, vec![0, 1, 2]);