Struct CancellationToken
pub struct CancellationToken { /* private fields */ }Expand description
Re-export of the protocol-layer cancellation primitive. Grammar
callers access it under this name; lower crates (search loops in
lattice-core) use lattice_protocol::CancellationToken
directly. The two are the same type.
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§
Source§impl CheckCancelled for CancellationToken
impl CheckCancelled for CancellationToken
Source§fn check(&self) -> GrammarResult<()>
fn check(&self) -> GrammarResult<()>
Ok(()) while the token is live; CommandError::Cancelled once
it has been cancelled. Cheap (one atomic load) – call it every
inner-loop iteration.§impl Clone for CancellationToken
impl Clone for CancellationToken
§fn clone(&self) -> CancellationToken
fn clone(&self) -> CancellationToken
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more