Skip to main content

CheckCancelled

Trait CheckCancelled 

Source
pub trait CheckCancelled {
    // Required method
    fn check(&self) -> GrammarResult<()>;
}
Expand description

Grammar extension: convert a flipped token into a CommandError::Cancelled result, so an evaluator’s inner loop threads cancellation through with ?.

§Examples

use lattice_grammar::{CancellationToken, CheckCancelled, CommandError, GrammarResult};

fn scan(lines: &[&str], cancel: &CancellationToken) -> GrammarResult<usize> {
    let mut n = 0;
    for line in lines {
        cancel.check()?; // poll once per iteration
        n += line.len();
    }
    Ok(n)
}

let token = CancellationToken::new();
assert_eq!(scan(&["ab", "c"], &token).unwrap(), 3);

// The UI flips the token (user Esc); the evaluator bails out and the
// dispatcher commits no effect.
token.cancel();
assert!(matches!(scan(&["ab"], &token), Err(CommandError::Cancelled)));

Required Methods§

Source

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.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§