#stream #future #generator #value #iterator #async #yield

macro no-std stream-future-impl

Implement an async iterator with return value

6 releases (3 breaking)

0.4.0 Mar 21, 2023
0.3.1 Dec 24, 2022
0.3.0 Aug 31, 2022
0.2.0 Aug 29, 2022
0.1.1 Aug 26, 2022

#33 in #yield

Download history 22/week @ 2023-12-10 117/week @ 2023-12-17 33/week @ 2023-12-24 8/week @ 2023-12-31 16/week @ 2024-01-07 19/week @ 2024-01-14 1/week @ 2024-01-21 2/week @ 2024-01-28 18/week @ 2024-02-04 24/week @ 2024-02-11 27/week @ 2024-02-18 53/week @ 2024-02-25 13/week @ 2024-03-03 26/week @ 2024-03-10 29/week @ 2024-03-17 107/week @ 2024-03-24

177 downloads per month
Used in 3 crates (via stream-future)

MIT license

9KB
118 lines

stream-future

crates.io docs.rs

This is a no_std compatible library to author a Future with Stream implemented. You can author simply with await and yield.

A nightly feature generators is required.

#![feature(generators)]

use stream_future::stream;

#[derive(Debug)]
enum Prog {
    Stage1,
    Stage2,
}

#[stream(Prog)]
async fn foo() -> Result<i32> {
    yield Prog::Stage1;
    // some works...
    yield Prog::Stage2;
    // some other works...
    Ok(0)
}

use tokio_stream::StreamExt;

let bar = foo();
tokio::pin!(bar);
while let Some(prog) = bar.next().await {
    println!("{:?}", prog);
}
let bar = bar.await?;
assert_eq!(bar, 0);
#![feature(generators)]

use stream_future::try_stream;

#[derive(Debug)]
enum Prog {
    Stage1,
    Stage2,
}

#[try_stream(Prog)]
async fn foo() -> Result<()> {
    yield Prog::Stage1;
    // some works...
    yield Prog::Stage2;
    // some other works...
    Ok(())
}

let bar = foo();
tokio::pin!(bar);
while let Some(prog) = bar.try_next().await? {
    println!("{:?}", prog);
}

You can specify the yield type in the attribute. Either the yield type or return type could be (). You can simply await other futures, and the macro will handle that.

Compare with async-stream

You can return any value you like! The caller can simply await and get the value without iterate the stream.

This library is 7x faster than async-stream, according to our benchmark.

Dependencies

~320–770KB
~18K SLoC