use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Clone)]
pub struct StopToken {
cancelled: Arc<AtomicBool>,
}
impl StopToken {
pub fn new() -> Self {
StopToken {
cancelled: Arc::new(AtomicBool::new(false)),
}
}
pub fn cancel(&self) {
self.cancelled.store(true, Ordering::SeqCst);
}
pub fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::SeqCst)
}
}
impl Default for StopToken {
fn default() -> Self {
StopToken::new()
}
}
impl std::fmt::Debug for StopToken {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("StopToken")
.field("cancelled", &self.is_cancelled())
.finish()
}
}