pub trait ContextStreamExt<Stream> {
// Required method
fn with_context<'a>(
self,
ctx: impl Into<ContextRef<'a>>,
) -> StreamWithContext<'a, Stream>
where Self: Sized;
}
Expand description
Extends a stream with useful functions.
Required Methods§
Sourcefn with_context<'a>(
self,
ctx: impl Into<ContextRef<'a>>,
) -> StreamWithContext<'a, Stream>where
Self: Sized,
fn with_context<'a>(
self,
ctx: impl Into<ContextRef<'a>>,
) -> StreamWithContext<'a, Stream>where
Self: Sized,
Wraps a stream with a context and stops the stream when the context is done.
§Example
let (ctx, handler) = Context::new();
tokio::spawn(async {
futures::stream::iter(1..=10).then(|d| async move {
// Do some work
tokio::time::sleep(std::time::Duration::from_secs(d)).await;
}).with_context(ctx);
});
// Will stop the spawned task and cancel all associated streams.
handler.cancel();