use std::sync::{
Arc,
atomic::{AtomicBool, Ordering},
};
#[derive(Clone, Debug, Default)]
pub struct ParseControl {
cancelled: Arc<AtomicBool>,
}
impl ParseControl {
pub fn new() -> Self {
Self::default()
}
pub fn cancel(&self) {
self.cancelled.store(true, Ordering::Relaxed);
}
pub fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::Relaxed)
}
pub(crate) fn check(&self) -> Result<(), ParseCancelled> {
if self.is_cancelled() {
Err(ParseCancelled)
} else {
Ok(())
}
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct ParseCancelled;
#[cfg(test)]
mod tests {
use super::ParseControl;
#[test]
fn cloned_control_observes_cancellation() {
let control = ParseControl::new();
let worker_control = control.clone();
control.cancel();
assert!(worker_control.is_cancelled());
assert!(worker_control.check().is_err());
}
}