cancel_this/triggers/
mod.rs

1use dyn_clone::{DynClone, clone_trait_object};
2
3mod timer;
4pub use timer::*;
5
6mod chain;
7pub use chain::*;
8
9mod never;
10pub use never::*;
11
12mod atomic;
13pub use atomic::*;
14
15#[cfg(feature = "memory")]
16mod memory;
17#[cfg(feature = "memory")]
18pub use memory::*;
19
20#[cfg(feature = "ctrlc")]
21mod ctrlc;
22#[cfg(feature = "ctrlc")]
23pub use ctrlc::*;
24
25#[cfg(feature = "pyo3")]
26mod pyo3;
27#[cfg(feature = "pyo3")]
28pub use pyo3::*;
29
30/// Defines an object that can be used to trigger cancellation.
31///
32/// In general, we only require that the object can be shared between threads and that it
33/// can be safely cloned.
34///
35/// **The expectation is that cloning a cancellation trigger produces an object that reacts to
36/// the same signal, i.e., is canceled if and only if the original object is canceled.**
37///
38pub trait CancellationTrigger: Send + Sync + DynClone {
39    /// Returns true if this trigger is canceled.
40    ///
41    /// In normal conditions, once a trigger is canceled, it stays canceled and should
42    /// not be able to reset.
43    fn is_cancelled(&self) -> bool;
44
45    /// Return the type name of this [`CancellationTrigger`], or in case of "composite"
46    /// triggers, *the type name of the trigger that actually signaled the cancellation*.
47    fn type_name(&self) -> &'static str;
48}
49
50clone_trait_object!(CancellationTrigger);
51
52/// A dynamic boxed [`CancellationTrigger`].
53pub type DynamicCancellationTrigger = Box<dyn CancellationTrigger>;
54
55impl CancellationTrigger for DynamicCancellationTrigger {
56    fn is_cancelled(&self) -> bool {
57        self.as_ref().is_cancelled()
58    }
59
60    fn type_name(&self) -> &'static str {
61        self.as_ref().type_name()
62    }
63}