Struct CancellationToken
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§
§impl CancellationToken
impl CancellationToken
pub fn new() -> CancellationToken
pub fn new() -> CancellationToken
Build a fresh, un-cancelled token.
pub fn never() -> CancellationToken
pub fn never() -> CancellationToken
Build a token that can never be cancelled. Useful as a default for callers that don’t drive cancellation.
pub fn cancel(&self)
pub fn cancel(&self)
Flip the token. Subsequent is_cancelled calls (on this
clone or any other) will observe the cancellation.
pub 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§
§impl CheckCancelled for CancellationToken
impl CheckCancelled for CancellationToken
§impl Clone for CancellationToken
impl Clone for CancellationToken
§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 more§impl Debug for CancellationToken
impl Debug for CancellationToken
§impl Default for CancellationToken
impl Default for CancellationToken
§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