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
//! \[WIP] Async-first signal handling, such as `CTRL` + `C`.
//!
//! Because this library is async-first, it should be more efficient than using
//! an alternative approach that awaits a blocking signal handler.

#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]

use std::task::{Context, Poll};

#[macro_use]
mod macros;

mod util;

cfg_unix! {
    pub mod unix;
}

#[cfg(windows)]
mod windows;

/// A future for `CTRL` + `C` signals.
#[derive(Debug)]
pub struct CtrlC {
    _private: (),
}

impl CtrlC {
    /// Receive the next signal notification event.
    #[inline]
    pub async fn recv(&mut self) -> Option<()> {
        util::poll_fn(|cx| self.poll_recv(cx)).await
    }

    /// Poll to receive the next signal notification event, outside of an
    /// `async` context.
    pub fn poll_recv(&mut self, _cx: &mut Context<'_>) -> Poll<Option<()>> {
        unimplemented!()
    }
}

cfg_stream! {
    impl futures::stream::Stream for CtrlC {
        type Item = ();

        #[inline]
        fn poll_next(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut Context<'_>
        ) -> Poll<Option<()>> {
            self.poll_recv(cx)
        }
    }
}