crb_runtime/
interruptor.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use futures::stream::AbortHandle;

// TODO: Use bit flags instead!
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub struct InterruptionLevel(u32);

impl InterruptionLevel {
    pub const EVENT: Self = Self::custom(100);
    pub const FLAG: Self = Self::custom(1_000);
    pub const ABORT: Self = Self::custom(10_000);

    pub const fn custom(value: u32) -> Self {
        Self(value)
    }

    pub fn next(&self) -> InterruptionLevel {
        if *self < Self::EVENT {
            Self::EVENT
        } else if *self < Self::FLAG {
            Self::FLAG
        } else if *self < Self::ABORT {
            Self::ABORT
        } else {
            Self::ABORT
        }
    }
}

pub trait Interruptor: Send + 'static {
    // TODO: Add levels?
    fn interrupt(&self);

    fn interrupt_with_level(&self, _level: InterruptionLevel) {
        self.interrupt();
    }
}

impl Interruptor for AbortHandle {
    fn interrupt(&self) {
        self.abort();
    }
}