CancellationTokenWrapper

Struct CancellationTokenWrapper 

Source
pub struct CancellationTokenWrapper {
    pub token: CancellationToken,
}

Fields§

§token: CancellationToken

Implementations§

Source§

impl CancellationTokenWrapper

Source

pub const js_cancel: __impl_cancel = __impl_cancel

Source§

impl CancellationTokenWrapper

Source

pub fn cancel(&self)

Source

pub fn new()

Methods from Deref<Target = CancellationToken>§

Source

pub fn child_token(&self) -> CancellationToken

Creates a CancellationToken which will get cancelled whenever the current token gets cancelled. Unlike a cloned CancellationToken, cancelling a child token does not cancel the parent token.

If the current token is already cancelled, the child token will get returned in cancelled state.

§Examples
use tokio::select;
use tokio_util::sync::CancellationToken;

#[tokio::main]
async fn main() {
    let token = CancellationToken::new();
    let child_token = token.child_token();

    let join_handle = tokio::spawn(async move {
        // Wait for either cancellation or a very long time
        select! {
            _ = child_token.cancelled() => {
                // The token was cancelled
                5
            }
            _ = tokio::time::sleep(std::time::Duration::from_secs(9999)) => {
                99
            }
        }
    });

    tokio::spawn(async move {
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        token.cancel();
    });

    assert_eq!(5, join_handle.await.unwrap());
}
Source

pub fn cancel(&self)

Cancel the CancellationToken and all child tokens which had been derived from it.

This will wake up all tasks which are waiting for cancellation.

Be aware that cancellation is not an atomic operation. It is possible for another thread running in parallel with a call to cancel to first receive true from is_cancelled on one child node, and then receive false from is_cancelled on another child node. However, once the call to cancel returns, all child nodes have been fully cancelled.

Source

pub fn is_cancelled(&self) -> bool

Returns true if the CancellationToken is cancelled.

Source

pub fn cancelled(&self) -> WaitForCancellationFuture<'_>

Returns a Future that gets fulfilled when cancellation is requested.

Equivalent to:

async fn cancelled(&self);

The future will complete immediately if the token is already cancelled when this method is called.

§Cancellation safety

This method is cancel safe.

Source

pub fn drop_guard_ref(&self) -> DropGuardRef<'_>

Creates a DropGuardRef for this token.

Returned guard will cancel this token (and all its children) on drop unless disarmed.

Source

pub async fn run_until_cancelled<F>( &self, fut: F, ) -> Option<<F as Future>::Output>
where F: Future,

Runs a future to completion and returns its result wrapped inside of an Option unless the CancellationToken is cancelled. In that case the function returns None and the future gets dropped.

§Fairness

Calling this on an already-cancelled token directly returns None. For all subsequent polls, in case of concurrent completion and cancellation, this is biased towards the future completion.

§Cancellation safety

This method is only cancel safe if fut is cancel safe.

Trait Implementations§

Source§

impl Clone for CancellationTokenWrapper

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'js> ConstructorCreator<'js, CancellationTokenWrapper> for ConstructorCreate<CancellationTokenWrapper>

Source§

fn create_constructor(&self, ctx: &Ctx<'js>) -> Result<Option<Constructor<'js>>>

Source§

impl Debug for CancellationTokenWrapper

Source§

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

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

impl Deref for CancellationTokenWrapper

Source§

type Target = CancellationToken

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for CancellationTokenWrapper

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl From<CancellationToken> for CancellationTokenWrapper

Source§

fn from(value: CancellationToken) -> Self

Converts to this type from the input type.
Source§

impl From<CancellationTokenWrapper> for CancellationToken

Source§

fn from(value: CancellationTokenWrapper) -> Self

Converts to this type from the input type.
Source§

impl<'js> FromJs<'js> for CancellationTokenWrapper
where for<'a> CloneWrapper<'a, Self>: CloneTrait<Self>,

Source§

fn from_js(ctx: &Ctx<'js>, value: Value<'js>) -> Result<Self>

Source§

impl<'js> IntoJs<'js> for CancellationTokenWrapper

Source§

fn into_js(self, ctx: &Ctx<'js>) -> Result<Value<'js>>

Source§

impl<'js> JsClass<'js> for CancellationTokenWrapper

Source§

const NAME: &'static str = "CancellationToken"

The name the constructor has in JavaScript
Source§

type Mutable = Writable

Can the type be mutated while a JavaScript value. Read more
Source§

fn prototype(ctx: &Ctx<'js>) -> Result<Option<Object<'js>>>

Returns the class prototype,
Source§

fn constructor(ctx: &Ctx<'js>) -> Result<Option<Constructor<'js>>>

Returns a predefined constructor for this specific class type if there is one.
Source§

const CALLABLE: bool = false

Is this class a function.
Source§

fn call<'a>( this: &JsCell<'js, Self>, params: Params<'a, 'js>, ) -> Result<Value<'js>, Error>

The function which will be called if Self::CALLABLE is true and an an object with this class is called as if it is a function.
Source§

impl<'js> JsLifetime<'js> for CancellationTokenWrapper

Source§

type Changed<'to> = CancellationTokenWrapper

The target which has the same type as a Self but with another lifetime 't
Source§

impl MethodImplementor<CancellationTokenWrapper> for MethodImpl<CancellationTokenWrapper>

Source§

fn implement(&self, _proto: &Object<'_>) -> Result<()>

Source§

impl<'js> Trace<'js> for CancellationTokenWrapper

Source§

fn trace<'a>(&self, _tracer: Tracer<'a, 'js>)

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<'js, T> AsProperty<'js, T> for T
where T: IntoJs<'js>,

Source§

fn config( self, ctx: &Ctx<'js>, ) -> Result<(i32, Value<'js>, Value<'js>, Value<'js>), Error>

Property configuration 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<'js, T> FromParam<'js> for T
where T: FromJs<'js>,

Source§

fn param_requirement() -> ParamRequirement

The parameters requirements this value requires.
Source§

fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<T, Error>

Convert from a parameter value.
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<'js, T> IntoArg<'js> for T
where T: IntoJs<'js>,

Source§

fn num_args(&self) -> usize

The number of arguments this value produces.
Source§

fn into_arg(self, args: &mut Args<'js>) -> Result<(), Error>

Convert the value into an argument.
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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 = 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.
Source§

impl<T> ParallelSend for T
where T: Send,