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
use stream_cancel::Trigger;

#[allow(clippy::module_name_repetitions)]
pub struct DisabledTrigger {
    trigger: Option<Trigger>,
}

impl DisabledTrigger {
    #[must_use]
    pub fn new(t: Trigger) -> Self {
        Self { trigger: Some(t) }
    }

    #[must_use]
    pub fn into_inner(mut self) -> Trigger {
        self.trigger.take().unwrap_or_else(|| unreachable!())
    }
}

impl Drop for DisabledTrigger {
    fn drop(&mut self) {
        if let Some(trigger) = self.trigger.take() {
            trigger.disable();
        }
    }
}

impl From<Trigger> for DisabledTrigger {
    fn from(t: Trigger) -> Self {
        Self::new(t)
    }
}