Trait ContextFutExt

Source
pub trait ContextFutExt<Fut> {
    // Required method
    fn with_context<'a>(
        self,
        ctx: impl Into<ContextRef<'a>>,
    ) -> FutureWithContext<'a, Fut> 
       where Self: Sized;
}
Expand description

Extends a future with useful functions.

Required Methods§

Source

fn with_context<'a>( self, ctx: impl Into<ContextRef<'a>>, ) -> FutureWithContext<'a, Fut>
where Self: Sized,

Wraps a future with a context and cancels the future when the context is done.

§Example
let (ctx, handler) = Context::new();

tokio::spawn(async {
   // Do some work
   tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}.with_context(ctx));

// Will stop the spawned task and cancel all associated futures.
handler.cancel();

Implementors§