pub fn fn_stream<T: Send, Fut: Future<Output = ()>>(
    func: impl FnOnce(StreamEmitter<T>) -> Fut
) -> FnStream<T, Fut>
Expand description

Create a new infallible stream which is implemented by func.

Caller should pass an async function which will return successive stream elements via StreamEmitter::emit.

Example

use async_fn_stream::fn_stream;
use futures::Stream;

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