#[repr(C)]pub struct CancellationTokenSource { /* private fields */ }
Implementations§
Source§impl CancellationTokenSource
impl CancellationTokenSource
pub fn new() -> CancellationTokenSource
Sourcepub fn token(&self) -> &Arc<CancellationToken>
pub fn token(&self) -> &Arc<CancellationToken>
Gets the token managed by this CancellationTokenSource.
The Arc
can be cloned so that the token can be passed into
asynchronous tasks that might outlive the CancellationTokenSource.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Marks the cancellation token as canceled.
Executes the on_cancel
callback of any active CancellationToken::run
invocations.
Has no effect if the cancellation token was already canceled.
Sourcepub fn cancel_after(&self, dur: Duration)
pub fn cancel_after(&self, dur: Duration)
Creates a new, detached thread that waits for the specified duration and then marks the cancellation token as canceled.
Methods from Deref<Target = CancellationToken>§
Sourcepub fn is_canceled(&self) -> bool
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).
Sourcepub fn result(&self) -> Result<(), OperationCanceled>
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.
Sourcepub fn run<C, F, R>(&self, on_cancel: C, f: F) -> R
pub fn run<C, F, R>(&self, on_cancel: C, f: F) -> 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.