crb_runtime/
interruptor.rs

1use futures::stream::AbortHandle;
2
3// TODO: Use bit flags instead!
4#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
5pub struct InterruptionLevel(u32);
6
7impl InterruptionLevel {
8    pub const EVENT: Self = Self::custom(100);
9    pub const FLAG: Self = Self::custom(1_000);
10    pub const ABORT: Self = Self::custom(10_000);
11    pub const EXIT: Self = Self::custom(100_000);
12
13    pub const fn custom(value: u32) -> Self {
14        Self(value)
15    }
16
17    pub fn next(&self) -> InterruptionLevel {
18        if *self < Self::EVENT {
19            Self::EVENT
20        } else if *self < Self::FLAG {
21            Self::FLAG
22        } else if *self < Self::ABORT {
23            Self::ABORT
24        } else {
25            Self::EXIT
26        }
27    }
28}
29
30pub trait Interruptor: Send + 'static {
31    // TODO: Add levels?
32    fn interrupt(&self);
33
34    fn interrupt_with_level(&self, _level: InterruptionLevel) {
35        self.interrupt();
36    }
37}
38
39impl Interruptor for AbortHandle {
40    fn interrupt(&self) {
41        self.abort();
42    }
43}