pub trait WellKnownProviderExt: Future + Sized {
    fn with_timeout(self, duration: Duration) -> ProviderFut<Self, Deadline>  { ... }
    fn with_deadline(self, deadline: Instant) -> ProviderFut<Self, Deadline>  { ... }
    fn with_shutdown_handler(
        self,
        handler: ShutdownReceiver
    ) -> ProviderFut<Self, ShutdownReceiver> { ... } }
Available on crate feature std only.
Expand description

Extension trait to provide some well known context values

Provided Methods§

Wraps a Future so that it should expire within the given duration.

Note, this doesn’t guarantee that the future will stop executing, this is up to the implementation to respect the timeout.

use std::time::Duration;
use context_rs::well_known::{
    WellKnownProviderExt, ShutdownSender, run_until_signal, SignalOrComplete
};

async fn do_something() {
    loop {
        // pretend this is more interesting
        let work = tokio::time::sleep(Duration::from_secs(1));
        match run_until_signal(std::pin::pin!(work)).await {
            SignalOrComplete::Completed(_) => continue,
            SignalOrComplete::ShutdownSignal(_) => break,
        }
    }
}

do_something().with_timeout(Duration::from_secs(5)).await;

Wraps a Future o that it should expire at the given deadline.

See with_timeout for more

Wraps a Future so it can be shutdown externally.

use context_rs::well_known::{
    WellKnownProviderExt, ShutdownSender, run_until_signal, SignalOrComplete
};

async fn generator(tx: tokio::sync::mpsc::Sender<i32>) {
    for i in 0.. {
        match run_until_signal(std::pin::pin!(tx.send(i))).await {
            SignalOrComplete::Completed(_) => continue,
            SignalOrComplete::ShutdownSignal(_) => break,
        }
    }
}

let shutdown = ShutdownSender::new();
let (tx, mut rx) = tokio::sync::mpsc::channel(1);

tokio::spawn(generator(tx).with_shutdown_handler(shutdown.clone().receiver()));

while let Some(x) = rx.recv().await {
    dbg!(x);
    if x == 5 {
        break
    }
}

// shutdown now that we're done with the rx
shutdown.shutdown()

Implementors§