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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use std::{marker::Unpin, ops::ControlFlow};

use futures::future::Future;
use tokio::sync::oneshot;

use crate::{InterruptSignal, InterruptibleFutureControl, InterruptibleFutureResult};

/// Provides the `.interruptible_control()` and `.interruptible_result()`
/// methods for `Future`s to return [`ControlFlow::Break`] or [`Result::Err`]
/// when an interrupt signal is received.
pub trait InterruptibleFutureExt<'rx, B, T> {
    fn interruptible_control(
        self,
        interrupt_rx: &'rx mut oneshot::Receiver<InterruptSignal>,
    ) -> InterruptibleFutureControl<'rx, B, T, Self>
    where
        Self: Sized + Future<Output = ControlFlow<B, T>>,
        B: From<(T, InterruptSignal)>;

    fn interruptible_result(
        self,
        interrupt_rx: &'rx mut oneshot::Receiver<InterruptSignal>,
    ) -> InterruptibleFutureResult<'rx, T, B, Self>
    where
        Self: Sized + Future<Output = Result<T, B>> + Unpin;

    #[cfg(feature = "ctrl_c")]
    fn interruptible_control_ctrl_c(self) -> InterruptibleFutureControl<'rx, B, T, Self>
    where
        Self: Sized + Future<Output = ControlFlow<B, T>>,
        B: From<(T, InterruptSignal)>;

    #[cfg(feature = "ctrl_c")]
    fn interruptible_result_ctrl_c(self) -> InterruptibleFutureResult<'rx, T, B, Self>
    where
        Self: Sized + Future<Output = Result<T, B>> + Unpin;
}

impl<'rx, B, T, Fut> InterruptibleFutureExt<'rx, B, T> for Fut
where
    Fut: Future,
{
    fn interruptible_control(
        self,
        interrupt_rx: &'rx mut oneshot::Receiver<InterruptSignal>,
    ) -> InterruptibleFutureControl<'rx, B, T, Self>
    where
        Self: Sized + Future<Output = ControlFlow<B, T>>,
        B: From<(T, InterruptSignal)>,
    {
        InterruptibleFutureControl::new(self, interrupt_rx.into())
    }

    fn interruptible_result(
        self,
        interrupt_rx: &'rx mut oneshot::Receiver<InterruptSignal>,
    ) -> InterruptibleFutureResult<'rx, T, B, Self>
    where
        Self: Sized + Future<Output = Result<T, B>> + Unpin,
    {
        InterruptibleFutureResult::new(self, interrupt_rx.into())
    }

    #[cfg(feature = "ctrl_c")]
    fn interruptible_control_ctrl_c(self) -> InterruptibleFutureControl<'rx, B, T, Self>
    where
        Self: Sized + Future<Output = ControlFlow<B, T>>,
        B: From<(T, InterruptSignal)>,
    {
        let (interrupt_tx, interrupt_rx) = oneshot::channel::<InterruptSignal>();

        tokio::task::spawn(async move {
            tokio::signal::ctrl_c()
                .await
                .expect("Failed to initialize signal handler for SIGINT");

            let (Ok(()) | Err(InterruptSignal)) = interrupt_tx.send(InterruptSignal);
        });

        InterruptibleFutureControl::new(self, interrupt_rx.into())
    }

    #[cfg(feature = "ctrl_c")]
    fn interruptible_result_ctrl_c(self) -> InterruptibleFutureResult<'rx, T, B, Self>
    where
        Self: Sized + Future<Output = Result<T, B>> + Unpin,
    {
        let (interrupt_tx, interrupt_rx) = oneshot::channel::<InterruptSignal>();

        tokio::task::spawn(async move {
            tokio::signal::ctrl_c()
                .await
                .expect("Failed to initialize signal handler for SIGINT");

            let (Ok(()) | Err(InterruptSignal)) = interrupt_tx.send(InterruptSignal);
        });

        InterruptibleFutureResult::new(self, interrupt_rx.into())
    }
}

#[cfg(test)]
mod tests {
    use std::ops::ControlFlow;

    use futures::FutureExt;
    use tokio::{join, sync::oneshot};

    use super::InterruptibleFutureExt;
    use crate::InterruptSignal;

    #[tokio::test]
    async fn interrupt_overrides_control_future_return_value() {
        let (interrupt_tx, mut interrupt_rx) = oneshot::channel::<InterruptSignal>();
        let (ready_tx, ready_rx) = oneshot::channel::<()>();

        let interruptible_control = async {
            let () = ready_rx.await.expect("Expected to be notified to start.");
            ControlFlow::Continue(())
        }
        .boxed()
        .interruptible_control(&mut interrupt_rx);

        let interrupter = async move {
            interrupt_tx
                .send(InterruptSignal)
                .expect("Expected to send `InterruptSignal`.");
            ready_tx
                .send(())
                .expect("Expected to notify sleep to start.");
        };

        let (control_flow, ()) = join!(interruptible_control, interrupter);

        assert_eq!(ControlFlow::Break(InterruptSignal), control_flow);
    }

    #[tokio::test]
    async fn interrupt_after_control_future_completes_does_not_override_value() {
        let (interrupt_tx, mut interrupt_rx) = oneshot::channel::<InterruptSignal>();

        let interruptible_control = async { ControlFlow::<InterruptSignal, ()>::Continue(()) }
            .boxed()
            .interruptible_control(&mut interrupt_rx);

        let interrupter = async move {
            let (Ok(()) | Err(InterruptSignal)) = interrupt_tx.send(InterruptSignal);
        };

        let (control_flow, ()) = join!(interruptible_control, interrupter);

        assert_eq!(ControlFlow::Continue(()), control_flow);
    }
}