pub struct CancellationTokenWrapper {
pub token: CancellationToken,
}Fields§
§token: CancellationTokenImplementations§
Methods from Deref<Target = CancellationToken>§
Sourcepub fn child_token(&self) -> CancellationToken
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());
}Sourcepub fn cancel(&self)
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.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns true if the CancellationToken is cancelled.
Sourcepub fn cancelled(&self) -> WaitForCancellationFuture<'_>
pub fn cancelled(&self) -> WaitForCancellationFuture<'_>
Sourcepub fn drop_guard_ref(&self) -> DropGuardRef<'_>
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.
Sourcepub async fn run_until_cancelled<F>(
&self,
fut: F,
) -> Option<<F as Future>::Output>where
F: Future,
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
impl Clone for CancellationTokenWrapper
Source§impl<'js> ConstructorCreator<'js, CancellationTokenWrapper> for ConstructorCreate<CancellationTokenWrapper>
impl<'js> ConstructorCreator<'js, CancellationTokenWrapper> for ConstructorCreate<CancellationTokenWrapper>
fn create_constructor(&self, ctx: &Ctx<'js>) -> Result<Option<Constructor<'js>>>
Source§impl Debug for CancellationTokenWrapper
impl Debug for CancellationTokenWrapper
Source§impl Deref for CancellationTokenWrapper
impl Deref for CancellationTokenWrapper
Source§impl DerefMut for CancellationTokenWrapper
impl DerefMut for CancellationTokenWrapper
Source§impl From<CancellationToken> for CancellationTokenWrapper
impl From<CancellationToken> for CancellationTokenWrapper
Source§fn from(value: CancellationToken) -> Self
fn from(value: CancellationToken) -> Self
Source§impl From<CancellationTokenWrapper> for CancellationToken
impl From<CancellationTokenWrapper> for CancellationToken
Source§fn from(value: CancellationTokenWrapper) -> Self
fn from(value: CancellationTokenWrapper) -> Self
Source§impl<'js> FromJs<'js> for CancellationTokenWrapperwhere
for<'a> CloneWrapper<'a, Self>: CloneTrait<Self>,
impl<'js> FromJs<'js> for CancellationTokenWrapperwhere
for<'a> CloneWrapper<'a, Self>: CloneTrait<Self>,
Source§impl<'js> IntoJs<'js> for CancellationTokenWrapper
impl<'js> IntoJs<'js> for CancellationTokenWrapper
Source§impl<'js> JsClass<'js> for CancellationTokenWrapper
impl<'js> JsClass<'js> for CancellationTokenWrapper
Source§fn constructor(ctx: &Ctx<'js>) -> Result<Option<Constructor<'js>>>
fn constructor(ctx: &Ctx<'js>) -> Result<Option<Constructor<'js>>>
Source§impl<'js> JsLifetime<'js> for CancellationTokenWrapper
impl<'js> JsLifetime<'js> for CancellationTokenWrapper
Source§type Changed<'to> = CancellationTokenWrapper
type Changed<'to> = CancellationTokenWrapper
Self but with another lifetime 'tSource§impl MethodImplementor<CancellationTokenWrapper> for MethodImpl<CancellationTokenWrapper>
impl MethodImplementor<CancellationTokenWrapper> for MethodImpl<CancellationTokenWrapper>
Auto Trait Implementations§
impl Freeze for CancellationTokenWrapper
impl RefUnwindSafe for CancellationTokenWrapper
impl Send for CancellationTokenWrapper
impl Sync for CancellationTokenWrapper
impl Unpin for CancellationTokenWrapper
impl UnwindSafe for CancellationTokenWrapper
Blanket Implementations§
Source§impl<'js, T> AsProperty<'js, T> for Twhere
T: IntoJs<'js>,
impl<'js, T> AsProperty<'js, T> for Twhere
T: IntoJs<'js>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<'js, T> FromParam<'js> for Twhere
T: FromJs<'js>,
impl<'js, T> FromParam<'js> for Twhere
T: FromJs<'js>,
Source§fn param_requirement() -> ParamRequirement
fn param_requirement() -> ParamRequirement
Source§fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<T, Error>
fn from_param<'a>(params: &mut ParamsAccessor<'a, 'js>) -> Result<T, Error>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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