https://github.com/berrysoft/stream-future
Implement Future + Stream with generators.
https://github.com/berrysoft/stream-future
asynchronous futures generators iterators rust stream
Last synced: about 1 month ago
JSON representation
Implement Future + Stream with generators.
- Host: GitHub
- URL: https://github.com/berrysoft/stream-future
- Owner: Berrysoft
- License: mit
- Created: 2022-08-26T00:58:47.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-21T05:15:33.000Z (over 1 year ago)
- Last Synced: 2025-08-17T02:54:08.513Z (about 2 months ago)
- Topics: asynchronous, futures, generators, iterators, rust, stream
- Language: Rust
- Homepage:
- Size: 29.3 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stream-future
[](https://crates.io/crates/stream-future)
[](https://docs.rs/stream-future)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 `coroutines` is required.
``` rust
#![feature(coroutines)]use stream_future::stream;
#[derive(Debug)]
enum Prog {
Stage1,
Stage2,
}#[stream(Prog)]
async fn foo() -> Result {
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);
`````` rust
#![feature(coroutines)]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.