feat: add resettable cancellation token

This commit is contained in:
Jindřich Moravec 2024-01-24 23:25:02 +01:00
parent 4c826923a5
commit 7b79dd69b4

View file

@ -0,0 +1,34 @@
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
pub struct ResetCancelToken {
is_canceled: Arc<AtomicBool>,
}
impl ResetCancelToken {
pub fn new() -> Self {
Self {
is_canceled: Arc::new(AtomicBool::new(false)),
}
}
pub fn is_canceled(&self) -> bool {
self.is_canceled.load(Ordering::SeqCst)
}
pub fn cancel(&self) {
self.is_canceled.store(true, Ordering::SeqCst);
}
pub fn reset(&self) {
self.is_canceled.store(false, Ordering::SeqCst);
}
}
impl Clone for ResetCancelToken {
fn clone(&self) -> Self {
Self {
is_canceled: self.is_canceled.clone(),
}
}
}