pub struct Signals(/* private fields */);Expand description
A handle for subscribing to OS signals delivered through a shared channel.
Signals is cheaply cloneable (backed by an Arc). Only one Signals
instance may be active at a time per process; calling Signals::new while
another instance exists returns SignalError::AlreadyInitialized.
OS-level signal handlers are installed automatically by Signals::new,
so signals are delivered from the moment the value is returned. Call
subscribe to obtain a Receiver.
Dropping the last clone releases all resources including the background thread.
§Example
use signal_msg::Signals;
let signals = Signals::new().expect("failed to create signal handler");
let receiver = signals.subscribe();
match receiver.listen() {
Ok(sig) => println!("received: {}", sig),
Err(e) => eprintln!("error: {}", e),
}Implementations§
Source§impl Signals
impl Signals
Sourcepub fn new() -> Result<Self, SignalError>
pub fn new() -> Result<Self, SignalError>
Creates a new signal channel and installs OS-level signal handlers.
Allocates a self-pipe, spawns a background dispatch thread named
signal-msg, and registers handlers for all supported signals. Call
subscribe to obtain a Receiver.
§Examples
let signals = signal_msg::Signals::new().expect("signal setup failed");
let receiver = signals.subscribe();§Errors
Returns SignalError::AlreadyInitialized if another Signals instance
is already active. Returns SignalError::OsError if an OS-level
operation fails (pipe creation, fcntl, or thread spawn).
Sourcepub fn subscribe(&self) -> Receiver
pub fn subscribe(&self) -> Receiver
Returns a new Receiver that will receive all subsequent signals.
Multiple independent receivers can be created from the same Signals
handle; each receives its own copy of every delivered signal.
§Examples
use signal_msg::Signals;
let signals = Signals::new().expect("signal setup failed");
let r1 = signals.subscribe();
let r2 = signals.subscribe();
// r1 and r2 each receive independent copies of every signal.