Crate stream_future
source ·Expand description
A Future
with item yielded.
#![feature(generators)]
#[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)
}
let bar = foo();
tokio::pin!(bar);
while let Some(prog) = bar.next().await {
println!("{:?}", prog);
}
let bar = bar.await?;
assert_eq!(bar, 0);
If a lifetime is needed, specify it in the attribute:
#![feature(generators)]
enum Prog {
Stage1,
Stage2,
}
#[stream(Prog, lifetime = 'a)]
async fn foo<'a>(s: &'a str) {
yield Prog::Stage1;
println!("{}", s);
yield Prog::Stage2;
}
foo("Hello world!").await;
There’s also a macro try_stream
(usually used) to implement a stream iterates Result
.
#![feature(generators)]
#[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);
}
Re-exports
pub use futures_core::Stream;
pub use stream_future_impl::stream;
pub use stream_future_impl::try_stream;