pub struct CancellationToken { /* private fields */ }Expand description
A token that can be checked for cancellation or awaited.
§Example
use async_cancellation_token::CancellationTokenSource;
use futures::{FutureExt, executor::LocalPool, task::LocalSpawnExt};
let cts = CancellationTokenSource::new();
let token = cts.token();
let mut pool = LocalPool::new();
pool.spawner().spawn_local(async move {
token.cancelled().await;
println!("Cancelled!");
}.map(|_| ())).unwrap();
cts.cancel();
pool.run();Implementations§
Source§impl CancellationToken
impl CancellationToken
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Check if the token has been cancelled.
Sourcepub fn check_cancelled(&self) -> Result<(), Cancelled>
pub fn check_cancelled(&self) -> Result<(), Cancelled>
Synchronously check cancellation and return Err(Cancelled) if cancelled.
Sourcepub fn cancelled(&self) -> CancelledFuture ⓘ
pub fn cancelled(&self) -> CancelledFuture ⓘ
Returns a Future that completes when the token is cancelled.
§Example
use async_cancellation_token::CancellationTokenSource;
use futures::{FutureExt, executor::LocalPool, task::LocalSpawnExt};
let cts = CancellationTokenSource::new();
let token = cts.token();
let mut pool = LocalPool::new();
pool.spawner().spawn_local(async move {
token.cancelled().await;
println!("Cancelled!");
}.map(|_| ())).unwrap();
cts.cancel();
pool.run();Sourcepub fn register(
&self,
f: impl FnOnce() + 'static,
) -> Option<CancellationTokenRegistration>
pub fn register( &self, f: impl FnOnce() + 'static, ) -> Option<CancellationTokenRegistration>
Register a callback to run when the token is cancelled.
- If the token is already cancelled, the callback is called immediately.
- Otherwise, the callback is stored and will be called exactly once upon cancellation.
Returns a CancellationTokenRegistration, which will remove the callback if dropped
before cancellation.
§Example
use std::{cell::Cell, rc::Rc};
use async_cancellation_token::CancellationTokenSource;
let cts = CancellationTokenSource::new();
let token = cts.token();
let flag = Rc::new(Cell::new(false));
let flag_clone = Rc::clone(&flag);
let reg = token.register(move || {
flag_clone.set(true);
});
cts.cancel();
assert!(flag.get());
drop(reg);Trait Implementations§
Source§impl Clone for CancellationToken
impl Clone for CancellationToken
Source§fn clone(&self) -> CancellationToken
fn clone(&self) -> CancellationToken
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 CancellationToken
impl !RefUnwindSafe for CancellationToken
impl !Send for CancellationToken
impl !Sync for CancellationToken
impl Unpin for CancellationToken
impl !UnwindSafe for CancellationToken
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