diskann_quantization/
cancel.rs1use std::sync::atomic::{AtomicBool, Ordering};
9
10pub trait Cancelation {
15 fn should_cancel(&self) -> bool;
16}
17
18pub struct AtomicCancelation<'a>(&'a AtomicBool);
20
21impl<'a> AtomicCancelation<'a> {
22 pub fn new(val: &'a AtomicBool) -> Self {
23 Self(val)
24 }
25}
26
27impl Cancelation for AtomicCancelation<'_> {
28 fn should_cancel(&self) -> bool {
29 self.0.load(Ordering::Relaxed)
30 }
31}
32
33pub struct DontCancel;
35
36impl Cancelation for DontCancel {
37 fn should_cancel(&self) -> bool {
38 false
39 }
40}
41
42#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn test_atomic_cancelation() {
52 let x = AtomicBool::new(false);
53 let y = AtomicCancelation::new(&x);
54 assert!(!y.should_cancel());
55 x.store(true, Ordering::Relaxed);
56 assert!(y.should_cancel());
57 }
58
59 #[test]
60 fn test_no_cancelation() {
61 let x = DontCancel;
62 assert!(!x.should_cancel());
63 }
64}