Skip to main content

Signals

Struct Signals 

Source
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");
for sig in signals.subscribe() {
    println!("received: {}", sig);
    if sig.is_terminating() { break; }
}

Implementations§

Source§

impl Signals

Source

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).

Examples found in repository?
examples/signal-msg-demo.rs (line 4)
3fn main() {
4    let signals = Signals::new().expect("failed to create signal handler");
5    println!("Waiting for signals...");
6    println!("(try SIGUSR1, SIGWINCH, SIGCONT; send SIGINT or SIGTERM to exit)\n");
7
8    for sig in signals.subscribe() {
9        println!("Got signal: {}", sig);
10        if sig.is_terminating() {
11            println!("\nTerminating on {}.", sig);
12            break;
13        }
14    }
15}
Source

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.
Examples found in repository?
examples/signal-msg-demo.rs (line 8)
3fn main() {
4    let signals = Signals::new().expect("failed to create signal handler");
5    println!("Waiting for signals...");
6    println!("(try SIGUSR1, SIGWINCH, SIGCONT; send SIGINT or SIGTERM to exit)\n");
7
8    for sig in signals.subscribe() {
9        println!("Got signal: {}", sig);
10        if sig.is_terminating() {
11            println!("\nTerminating on {}.", sig);
12            break;
13        }
14    }
15}

Trait Implementations§

Source§

impl Clone for Signals

Source§

fn clone(&self) -> Signals

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Signals

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.