Skip to main content

capsule_library/import/
executor_cancellation.rs

1use std::sync::Arc;
2use std::sync::atomic::{AtomicBool, Ordering};
3
4/// A token that can be used to cancel an in-progress import.
5#[derive(Clone, Default)]
6pub struct CancellationToken(Arc<AtomicBool>);
7
8impl CancellationToken {
9    pub fn new() -> Self {
10        Self(Arc::new(AtomicBool::new(false)))
11    }
12
13    /// Signal cancellation.
14    pub fn cancel(&self) {
15        self.0.store(true, Ordering::SeqCst);
16    }
17
18    /// Returns `true` if cancellation has been requested.
19    pub fn is_cancelled(&self) -> bool {
20        self.0.load(Ordering::SeqCst)
21    }
22}
23
24#[cfg(test)]
25mod tests;