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
//! Collection of functions generating Unix process signal receivers
//! for use in the [crossbeam_channel::select!] macro.
//!
//! ## Example
//!
//! ```
//! let tick = tick(Duration::from_secs(1));
//! let stop = interruption_or_termination();
//!
//! loop {
//!     select! {
//!         recv(tick) -> _ => println!("Running!"),
//!         recv(stop) -> _ => break,
//!     }
//! }
//! ```
//!

use crossbeam_channel::{unbounded, Receiver};
pub use simple_signal::Signal;

/// Create (single) receiver for Unix process signals listed in `signals`.
pub fn signal(signals: &[Signal]) -> Receiver<()> {
    let (sender, receiver) = unbounded();
    simple_signal::set_handler(signals, {
        move |_| {
            let _ = sender.send(());
        }
    });

    receiver
}

/// Create receiver for `SIGHUP` signal.
pub fn hang_up() -> Receiver<()> {
    signal(&[Signal::Hup])
}

/// Create receiver for `SIGINT` signal.
pub fn interruption() -> Receiver<()> {
    signal(&[Signal::Int])
}

/// Create receiver for `SIGTERM` signal.
pub fn termination() -> Receiver<()> {
    signal(&[Signal::Term])
}

/// Create (single) receiver for `SIGINT` and `SIGTERM` signal.
pub fn interruption_or_termination() -> Receiver<()> {
    signal(&[Signal::Int, Signal::Term])
}