use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
#[derive(Clone, Debug)]
pub struct Cancellation {
cancelled: Arc<AtomicBool>,
}
impl Cancellation {
pub(crate) fn new() -> (Cancellation, CancellationGuard) {
let cancelled = Arc::new(AtomicBool::new(false));
(
Cancellation {
cancelled: cancelled.clone(),
},
CancellationGuard { cancelled },
)
}
#[inline]
pub fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::Relaxed)
}
}
pub struct CancellationGuard {
cancelled: Arc<AtomicBool>,
}
impl Drop for CancellationGuard {
fn drop(&mut self) {
self.cancelled.store(true, Ordering::Relaxed);
}
}