pub struct CancellationToken { /* private fields */ }Expand description
Cooperative cancellation handle. Cheap to clone (one Arc bump); safe to share across threads / tasks.
Every clone shares one flag: cancelling any clone is observed by all of
them, and a cancelled token never resets. Cancellation is a request, not
preemption — the work only stops where it polls Self::is_cancelled.
§Examples
use lattice_protocol::CancellationToken;
let token = CancellationToken::new();
let worker_copy = token.clone(); // handed to the evaluator
let mut steps = 0;
for i in 0..1_000 {
if worker_copy.is_cancelled() {
break; // bail with the domain's "cancelled" error
}
steps += 1;
if i == 9 {
token.cancel(); // e.g. the user pressed Esc
}
}
assert_eq!(steps, 10);
assert!(worker_copy.is_cancelled());Implementations§
Source§impl CancellationToken
impl CancellationToken
Sourcepub fn never() -> Self
pub fn never() -> Self
Build a token that can never be cancelled. Useful as a default for callers that don’t drive cancellation.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Flip the token. Subsequent is_cancelled calls (on this
clone or any other) will observe the cancellation.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Cheap (one atomic load) check. Returns true iff the
token has been cancelled by some clone.
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 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CancellationToken
impl Debug for CancellationToken
Source§impl Default for CancellationToken
impl Default for CancellationToken
Source§fn default() -> CancellationToken
fn default() -> CancellationToken
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for CancellationToken
impl RefUnwindSafe for CancellationToken
impl Send for CancellationToken
impl Sync for CancellationToken
impl Unpin for CancellationToken
impl UnsafeUnpin 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