use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use crate::{GeomError, GeomResult};
#[derive(Debug, Clone, Default)]
pub struct CancellationToken {
flag: Arc<AtomicBool>,
}
impl PartialEq for CancellationToken {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.flag, &other.flag)
}
}
impl Eq for CancellationToken {}
impl CancellationToken {
pub fn new() -> Self {
Self::default()
}
pub fn cancel(&self) {
self.flag.store(true, Ordering::Relaxed);
}
pub fn is_cancelled(&self) -> bool {
self.flag.load(Ordering::Relaxed)
}
pub fn check(&self) -> GeomResult<()> {
if self.is_cancelled() {
return Err(GeomError::Cancelled);
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CancellationGranularity {
None,
BetweenOperations,
Incremental,
}