pub struct CancellationTokenSource { /* private fields */ }Expand description
A source that can cancel associated CancellationTokens.
Cancellation is cooperative and single-threaded. When cancelled:
- All registered
FnOncecallbacks are called (in registration order). - All futures waiting via
CancellationToken::cancelled()are woken.
§Example
use async_cancellation_token::CancellationTokenSource;
let cts = CancellationTokenSource::new();
let token = cts.token();
assert!(!cts.is_cancelled());
cts.cancel();
assert!(cts.is_cancelled());Implementations§
Source§impl CancellationTokenSource
impl CancellationTokenSource
Sourcepub fn token(&self) -> CancellationToken
pub fn token(&self) -> CancellationToken
Get a CancellationToken associated with this source.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancel all associated tokens.
This marks the source as cancelled. After cancellation:
- All registered callbacks are called exactly once.
- All waiting futures are woken.
Note: Cancellation is idempotent; calling this method multiple times has no effect. FnOnce callbacks will only be called once.
Single-threaded only. Not safe to call concurrently from multiple threads.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if this source has been cancelled.
Trait Implementations§
Source§impl Clone for CancellationTokenSource
impl Clone for CancellationTokenSource
Source§fn clone(&self) -> CancellationTokenSource
fn clone(&self) -> CancellationTokenSource
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for CancellationTokenSource
impl !RefUnwindSafe for CancellationTokenSource
impl !Send for CancellationTokenSource
impl !Sync for CancellationTokenSource
impl Unpin for CancellationTokenSource
impl !UnwindSafe for CancellationTokenSource
Blanket Implementations§
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
Mutably borrows from an owned value. Read more