Struct Token

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

A cancellation token. It tracks the state and holds an optional deadline for the operation. To share Token across threads, wrap it in a std::sync::Arc.

Implementations§

Source§

impl Token

Source

pub fn new() -> Self

Create a new Token with no deadline. The token will be marked as canceled only once Token::cancel has been called.

Source

pub fn with_duration(duration: Duration) -> Self

Create a new Token with a deadline set to the current clock plus the specified duration. The token will be marked as canceled either when Token::cancel is called, or when the operation calls either Token::is_canceled or Token::check_cancel and the current clock exceeds the computed deadline.

Source

pub fn with_deadline(deadline: Instant) -> Self

Create a new Token with a deadline set to the specified instant. The token will be marked as canceled either when Token::cancel is called, or when the operation calls either Token::is_canceled or Token::check_cancel and the current clock exceeds the specified deadline.

Source

pub fn cancel(&self)

Explicitly mark the token as being canceled. This method is async signal safe.

Source

pub fn was_canceled(&self) -> bool

Check whether the token was canceled. This method is intended to be called by code that initiated (rather than performed) an operation to test whether that operation was successful. If you want to test for cancellation in the body of your processing code you should use either Token::is_canceled or Token::check_cancel. Using Token::check_cancel to propagate a Result value is often a cleaner design than using Token::was_canceled.

Source

pub fn is_canceled(&self) -> bool

Test whether an ongoing operation should cease due to cancellation. If a deadline has been set, the current clock will be evaluated and compared against the deadline, setting the state to canceled if appropriate. Returns true if the operation has been canceled.

Source

pub fn check_cancel(&self) -> Result<(), Canceled>

Test whether an ongoing operation should cease due to cancellation, propagating a Canceled error value if the operation has been canceled. If a deadline has been set, the current clock will be evaluated and compared against the deadline, setting the state to canceled if appropriate.

Trait Implementations§

Source§

impl Debug for Token

Source§

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

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

impl Default for Token

Source§

fn default() -> Token

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

Auto Trait Implementations§

§

impl !Freeze for Token

§

impl RefUnwindSafe for Token

§

impl Send for Token

§

impl Sync for Token

§

impl Unpin for Token

§

impl UnwindSafe for Token

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.