Context

Struct Context 

Source
pub struct Context { /* private fields */ }
Expand description

A context that can be used to spawn tokio tasks Cancelling the context (or dropping it) will cancel all async tasks spawn by this context You can create child context too.

   use std::time::Duration;
   use tokio_tree_context::Context;
   async fn testing() {
       let mut ctx = Context::new();
        
       let mut ctx1 = ctx.new_child_context();
       let mut ctx12 = ctx1.new_child_context();
        
       ctx.spawn(async move {
           sleep("ctx".into(), 100).await;
       });
       ctx1.spawn(async move {
           sleep("ctx1".into(), 100).await;
       });
       ctx12.spawn(async move {
           sleep("ctx12".into(), 100).await;
       });
       println!("Cancelling CTX 1");
       drop(ctx1);
       sleep("main".into(), 5).await;
       println!("Cancelling CTX 12");
       drop(ctx12);
       sleep("main".into(), 5).await;
        
       println!("Cancelling CTX");
       drop(ctx);
        
       sleep("main".into(), 5).await;
        
   }
        
   async fn sleep(name:String, what: u64) {
       for i in 0..what {
           println!("Task {} sleeping {} out of {} seconds", name, i + 1, what);
           tokio::time::sleep(Duration::from_secs(1)).await;
           println!("Task {} awake", name);
       }
   }

Implementations§

Source§

impl Context

Source

pub fn cancel(self)

Cancel all tasks under this context

Source

pub fn new() -> Context

Create a new context

Source

pub fn with_parent(parent: &mut Context) -> Context

Create a new Context from a parent. Same as parent.new_child_context()

Source

pub fn new_child_context(&mut self) -> Context

Create a new child context, where cancelling the parent context, will also cancel the child context. Child context can have their own child context too.

The new context has a logical relationship with the parent. Cancelling parent will cancel child too.

Source

pub fn spawn_with_timeout<T>( &mut self, future: T, timeout: Option<Duration>, ) -> JoinHandle<Option<T::Output>>
where T: Future + Send + 'static, T::Output: Send + 'static,

Run a task with at timeout. If timeout is None, then no timeout is used Task will run until: The task is completed The timeout is reached The context is cancelled Any of the parent/ancestor context is cancelled

Which ever is earlier. For example

use std::time::Duration;
use tokio_tree_context::Context;
 
let mut ctx = Context::new();
ctx.spawn_with_timeout(async move {
    // do your work here
}, Some(Duration::from_secs(3))); // task cancels after 3 seconds
// wait sometime
ctx.cancel();
Source

pub fn spawn<T>(&mut self, future: T) -> JoinHandle<Option<T::Output>>
where T: Future + Send + 'static, T::Output: Send + 'static,

Spawn task without tiemout Task is cancelled when you call this context’s cancel or drop the context

For example

use std::time::Duration;
use tokio_tree_context::Context;
 
let mut ctx = Context::new();
ctx.spawn(async move {
    // do your work here
});
// wait sometime
ctx.cancel();

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.