cancellation

Struct CancellationToken

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

A CancellationToken is used to query whether an operation should be canceled.

To cancel a token, use CancellationTokenSource.

Use CancellationTokenSource to obtain instances of CancellationToken.

Implementations§

Source§

impl CancellationToken

Source

pub fn none() -> &'static CancellationToken

Returns a reference to a cancellation token that is never canceled.

Source

pub fn is_canceled(&self) -> bool

Gets whether this token has been canceled.

This function is inherently racy: it may start returning true at any moment as the token is canceled by another thread.

However, once the function returns true, it will always keep returning true (cancellation tokens cannot be reset).

Source

pub fn result(&self) -> Result<(), OperationCanceled>

Returns Ok(()) if this token has not been canceled. Returns Err(OperationCanceled) if this token has been canceled.

This is an alternative to is_canceled() that can be used with the try!() macro.

Source

pub fn run<C, F, R>(&self, on_cancel: C, f: F) -> R
where C: FnOnce() + Send, F: FnOnce() -> R,

Runs function f on the current thread. If the token is canceled while f is running, the on_cancel function will be executed by the thread calling cancel().

If the f callback returns before the on_cancel callback does, run() waits for the on_cancel callback to finish before returning. Any memory writes performed by the on_cancel callback will be visible after run() returns.

If the token is already canceled when this function is called, on_cancel will be executed on the current thread before f is called.

If the token is not canceled during the execution of f, the on_cancel callback will not be called at all.

Panics in the f callback are supported: unwinding will wait for on_cancel to finish (if it is running concurrently) and unregister the on_cancel callback from the token.

Panics in the on_cancel callback may abort the process.

Trait Implementations§

Source§

impl Debug for CancellationToken

Source§

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

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

impl Send for CancellationToken

Source§

impl Sync for CancellationToken

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.