feat(managed): implement thread-safe cancellation token

This commit is contained in:
Nils Andreas Svee committed 2026-04-28 23:34:34 +07:00
1 parent 90e7239157
commit b83674880c
1 file changed
+43
@@ -0,0 +1,43 @@
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
#[derive(Debug, Clone, Default)]
pub struct CancellationToken {
cancelled: Arc<AtomicBool>,
}
impl CancellationToken {
pub fn new() -> Self {
Self::default()
}
pub fn cancel(&self) {
self.cancelled.store(true, Ordering::SeqCst);
}
pub fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::SeqCst)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn token_starts_uncancelled() {
assert!(!CancellationToken::new().is_cancelled());
}
#[test]
fn cancel_is_visible_through_clones() {
let token = CancellationToken::new();
let clone = token.clone();
token.cancel();
assert!(clone.is_cancelled());
}
}