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;Trait Aliases
A convenience of StreamFuture which handles Try.
For example, TryStreamFuture<Result<R>, P> is StreamFuture<Result<R>, Result<P>>,
and TryStreamFuture<Option<R>, P> is StreamFuture<Option<R>, Option<P>>.