context_async

Trait Context

Source
pub trait Context:
    Clone
    + Send
    + Sync {
    // Required methods
    fn timer(&self) -> Timer;
    fn spawn<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn spawn_with_timeout<'life0, 'async_trait>(
        &'life0 self,
        timeout: Duration,
    ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn deadline<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Option<Instant>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn cancel<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn is_cancelled<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn is_timeout<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn handle<'a, 'life0, 'async_trait, Fut, Output>(
        &'life0 self,
        fut: Fut,
    ) -> Pin<Box<dyn Future<Output = Result<Output>> + Send + 'async_trait>>
       where Fut: Future<Output = Output> + Send + Sync + 'a + 'async_trait,
             Output: 'async_trait,
             Self: 'async_trait,
             'a: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

The Context trait defines the required methods for Context. It can define a duration, be cancellable, and immediately cancel async functions if the processing time exceeds the allowed duration without being cancelled. Additionally, the Context can spawn child contexts.

§Examples

use context_async::{Context, Timer};
async fn my_function() {}

let timer = Timer::background(); // a new context without duration limit///
timer.handle(my_function()).await.unwrap();

In addition to using handle, you can also handle it with the future.with() method by simply importing the With trait.

use context_async::{With, Context, Timer};
async fn my_function() {}

let timer = Timer::todo(); // same as background().
my_function()
    .with(timer)
    .await
    .unwrap();

Required Methods§

Source

fn timer(&self) -> Timer

return the basic Timer.

Source

fn spawn<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

spawn a new child context.

When the parent (self) is cancelled (call by Self::cancel), the child context will be cancelled too.

§Example

use context_async::{Context, Timer};

tokio_test::block_on(async {
let ctx = Timer::background();
let child = ctx.spawn().await;
let child_of_child = child.spawn().await;

ctx.cancel().await; // <- parent is cancelled.

assert!(child.is_cancelled().await); // the child is cancelled too.
assert!(child_of_child.is_cancelled().await); // also cancelled.
});
Source

fn spawn_with_timeout<'life0, 'async_trait>( &'life0 self, timeout: Duration, ) -> Pin<Box<dyn Future<Output = Self> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

spawn a new child context, with a new timeout parameter.

Same as Self::spawn, when the parent (self) is cancelled (call by Self::cancel), the child context will be cancelled too.

The expire_at instant should not be longer than the parent’s expire_at.

§Note

see Self::spawn for more examples.

Provided Methods§

Source

fn deadline<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Option<Instant>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

return the deadline time::Instant of this context. return None when this context doesn’t have deadline.

Source

fn cancel<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

cancel this context, then cancel all its childs.

Source

fn is_cancelled<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

check whether this context is cancelled or not.

Source

fn is_timeout<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = bool> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

check whether this context is timeout or not.

Source

fn handle<'a, 'life0, 'async_trait, Fut, Output>( &'life0 self, fut: Fut, ) -> Pin<Box<dyn Future<Output = Result<Output>> + Send + 'async_trait>>
where Fut: Future<Output = Output> + Send + Sync + 'a + 'async_trait, Output: 'async_trait, Self: 'async_trait, 'a: 'async_trait, 'life0: 'async_trait,

handle a future

§Examples
use context_async::{Context, Timer};

 let ctx = Timer::background();
 let task = tokio::time::sleep(tokio::time::Duration::from_secs(1));

 ctx.handle(task).await.unwrap();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§