use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::Notify;
#[derive(Clone)]
pub struct CancelToken {
pub(crate) requested: Arc<AtomicBool>,
pub(crate) notify: Arc<Notify>,
}
impl CancelToken {
pub fn new() -> Self {
Self {
requested: Arc::new(AtomicBool::new(false)),
notify: Arc::new(Notify::new()),
}
}
pub fn is_requested(&self) -> bool {
self.requested.load(Ordering::SeqCst)
}
pub fn cancel(&self) {
self.requested.store(true, Ordering::SeqCst);
self.notify.notify_waiters();
}
pub fn reset(&mut self) {
self.requested.store(false, Ordering::SeqCst);
self.notify = Arc::new(Notify::new());
}
pub fn notified(&self) -> impl std::future::Future<Output = ()> + '_ {
self.notify.notified()
}
}
impl Default for CancelToken {
fn default() -> Self {
Self::new()
}
}