#async-stream #stream #async

async-fn-stream

Lightweight implementation of async-stream without macros

4 releases

0.2.2 Apr 23, 2024
0.2.1 Apr 18, 2024
0.2.0 Aug 3, 2022
0.1.0 Jul 7, 2022

#91 in Asynchronous

Download history 2698/week @ 2024-08-14 2570/week @ 2024-08-21 3358/week @ 2024-08-28 3660/week @ 2024-09-04 4542/week @ 2024-09-11 3806/week @ 2024-09-18 4066/week @ 2024-09-25 3739/week @ 2024-10-02 7935/week @ 2024-10-09 10650/week @ 2024-10-16 9315/week @ 2024-10-23 9089/week @ 2024-10-30 9280/week @ 2024-11-06 6082/week @ 2024-11-13 4644/week @ 2024-11-20 2377/week @ 2024-11-27

23,725 downloads per month
Used in 13 crates (10 directly)

MIT license

18KB
305 lines

A version of async-stream without macros. This crate provides generic implementations of Stream trait. Stream is an asynchronous version of std::iter::Iterator.

Two functions are provided - fn_stream and try_fn_stream.

Usage

Basic Usage

If you need to create a stream that may result in error, use try_fn_stream, otherwise use fn_stream.

To create a stream:

  1. Invoke fn_stream or try_fn_stream, passing a closure (anonymous function).
  2. Closure will accept an emitter. To return value from the stream, call .emit(value) on emitter and .await on its result. Once stream consumer has processed the value and called .next() on stream, .await will return.

Returning errors

try_fn_stream provides some conveniences for returning errors:

  1. Errors can be return from closure via return Err(...) or the question mark (?) operator. This will end the stream.
  2. An emitter also has an emit_err() method to return errors without ending the stream.

Examples

Finite stream of numbers

use async_fn_stream::fn_stream;
use futures_util::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;
        }
    })
}

Read numbers from text file, with error handling

use anyhow::Context;
use async_fn_stream::try_fn_stream;
use futures_util::{pin_mut, Stream, StreamExt};
use tokio::{
    fs::File,
    io::{AsyncBufReadExt, BufReader},
};

fn read_numbers(file_name: String) -> impl Stream<Item = Result<i32, anyhow::Error>> {
    try_fn_stream(|emitter| async move {
        // Return errors via `?` operator.
        let file = BufReader::new(File::open(file_name).await.context("Failed to open file")?);
        pin_mut!(file);
        let mut line = String::new();
        loop {
            line.clear();
            let byte_count = file
                .read_line(&mut line)
                .await
                .context("Failed to read line")?;
            if byte_count == 0 {
                break;
            }

            for token in line.split_ascii_whitespace() {
                let Ok(number) = token.parse::<i32>() else {
                    // Return errors via the `emit_err` method.
                    emitter.emit_err(
                        anyhow::anyhow!("Failed to convert string \"{token}\" to number")
                    ).await;
                    continue;
                };
                emitter.emit(number).await;
            }
        }

        Ok(())
    })
}

Why not async-stream?

async-stream is great! It has a nice syntax, but it is based on macros which brings some flaws:

Dependencies

~560–760KB
~14K SLoC