Struct tokio_util::sync::CancellationToken
source · [−]pub struct CancellationToken { /* private fields */ }
Expand description
A token which can be used to signal a cancellation request to one or more tasks.
Tasks can call CancellationToken::cancelled()
in order to
obtain a Future which will be resolved when cancellation is requested.
Cancellation can be requested through the CancellationToken::cancel
method.
Examples
use tokio::select;
use tokio_util::sync::CancellationToken;
#[tokio::main]
async fn main() {
let token = CancellationToken::new();
let cloned_token = token.clone();
let join_handle = tokio::spawn(async move {
// Wait for either cancellation or a very long time
select! {
_ = cloned_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());
}
Implementations
sourceimpl CancellationToken
impl CancellationToken
sourcepub fn new() -> CancellationToken
pub fn new() -> CancellationToken
Creates a new CancellationToken in the non-cancelled state.
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.
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.
sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns true
if the CancellationToken
had been cancelled
sourcepub fn cancelled(&self) -> WaitForCancellationFuture<'_>ⓘNotable traits for WaitForCancellationFuture<'a>impl<'a> Future for WaitForCancellationFuture<'a> type Output = ();
pub fn cancelled(&self) -> WaitForCancellationFuture<'_>ⓘNotable traits for WaitForCancellationFuture<'a>impl<'a> Future for WaitForCancellationFuture<'a> type Output = ();
Returns a Future
that gets fulfilled when cancellation is requested.
sourcepub fn drop_guard(self) -> DropGuard
pub fn drop_guard(self) -> DropGuard
Creates a DropGuard
for this token.
Returned guard will cancel this token (and all its children) on drop unless disarmed.
Trait Implementations
sourceimpl Clone for CancellationToken
impl Clone for CancellationToken
sourceimpl Debug for CancellationToken
impl Debug for CancellationToken
sourceimpl Default for CancellationToken
impl Default for CancellationToken
sourcefn default() -> CancellationToken
fn default() -> CancellationToken
Returns the “default value” for a type. Read more
sourceimpl Drop for CancellationToken
impl Drop for CancellationToken
impl Send for CancellationToken
impl Sync for CancellationToken
Auto Trait Implementations
impl RefUnwindSafe for CancellationToken
impl Unpin for CancellationToken
impl UnwindSafe for CancellationToken
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more