Skip to main content

WaitToken

Struct WaitToken 

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

WaitToken (aka CancellationToken) is an object used for ‘cancellation’ events.

Example:

// Create a new token
let cancellation = WaitToken::new();

// Clone token to use it in another thread. You can call cancel on this object too!
let cancellation_clone = cancellation.clone();
tokio::spawn(async move {
    // wait for token to cancel
    cancellation_clone.await;
    println!("Finished");
})

// Child tokens will cancel when parent is cancelled.
// 'cancel()' on child tokens will not cancel parent
let child = cancellation.make_child_token();
tokio::spawn(async move {
    // Wait for token to cancel
    child.await;
    println!("Child is finished");
})

println!("Cancelled");
cancellation.cancel();

// This await is just for demonstration. No need to await after cancel
// Note that 'await' takes by-value so you may need to clone
cancellation.clone().await;

// Undo cancellation of token
cancellation.reset();

// This await will not return because token is not cancelled anymore
cancellation.clone().await;

// This is unreachable
println!("Something went wrong!");

Implementations§

Source§

impl WaitToken

Source

pub fn new() -> Self

Create a new token

Source

pub fn spawn_terminator(&self) -> JoinHandle<Result<(), Error>>

Spawns background fetch to listem SIGTERM & SIGINT events and calls cancel

Source

pub fn ready() -> Self

Create an already cancelled token

Source

pub fn cancel(&self) -> CancelState

Set state to ‘cancelled’. Futures waiting for token will complete and streams terminated.

Source

pub fn reset(&self)

Set state to ‘not cancelled’

Source

pub fn is_cancelled(&self) -> bool

Source

pub fn cancelled(&self) -> WaitTokenCancelled

Source

pub fn until_cancel(&self) -> TakeUntil<Repeat<()>, WaitTokenCancelled>

Make a stream that will emit items until token is cancelled

Source

pub fn on_cancel(&self) -> Once<WaitTokenCancelled>

Source

pub async fn on_cancel_then<Fut: Future + Send>(self, fut: Fut) -> Fut::Output

Source

pub fn repeat_until_cancel(&self, frq: Duration) -> WaitTokenRepeat<Self>

Make an object that will wait for delay every iteration and return false if token is cancelled. Common usage is with while loop.

Example:

let mut repeater = cancellation.repeat_until_cancel(Duration::from_secs(1));
while repeater.next().await {
    do_something();
}
Source

pub async fn run_fn<Fut: Future>(&self, fut: Fut) -> Option<Fut::Output>

Run future. Future will be aborted when token is cancelled

Source

pub fn make_child_token(&self) -> Self

Create a sub-token that will cancel if current token cancelled.
Cancel on child token will not cancel parent

Source

pub fn guard(&self) -> WaitTokenGuard

Create a guard that will cancel wait token on drop

Trait Implementations§

Source§

impl Clone for WaitToken

Source§

fn clone(&self) -> WaitToken

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WaitToken

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for WaitToken

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.