pub struct SignalsInfo<E: Exfiltrator = SignalOnly>(_);
Available on non-Windows and crate feature iterator only.
Expand description

The main structure of the module, representing interest in some signals.

Unlike the helpers in other modules, this registers the signals when created and unregisters them on drop. It provides the pending signals during its lifetime, either in batches or as an infinite iterator.

Most users will want to use it through the Signals type alias for simplicity.

Multiple threads

Instances of this struct can be sent to other threads. In a multithreaded application this can be used to dedicate a separate thread for signal handling. In this case you should get a Handle using the handle method before sending the Signals instance to a background thread. With the handle you will be able to shut down the background thread later, or to operatively add more signals.

The controller handle can be shared between as many threads as you like using its clone method.

Exfiltrators

The [SignalOnly] provides only the signal number. There are further exfiltrators available in the exfiltrator module. Note that some of them are behind feature flags that need to be enabled.

Examples

use signal_hook::consts::signal::*;
use signal_hook::iterator::Signals;

let mut signals = Signals::new(&[SIGUSR1, SIGUSR2])?;
let handle = signals.handle();
let thread = thread::spawn(move || {
    for signal in &mut signals {
        match signal {
            SIGUSR1 => {},
            SIGUSR2 => {},
            _ => unreachable!(),
        }
    }
});

// Some time later...
handle.close();
thread.join().unwrap();

Implementations§

source§

impl<E: Exfiltrator> SignalsInfo<E>

source

pub fn new<I, S>(signals: I) -> Result<Self, Error>where I: IntoIterator<Item = S>, S: Borrow<c_int>, E: Default,

Creates the Signals structure.

This registers all the signals listed. The same restrictions (panics, errors) apply as for the Handle::add_signal method.

source

pub fn with_exfiltrator<I, S>(signals: I, exfiltrator: E) -> Result<Self, Error>where I: IntoIterator<Item = S>, S: Borrow<c_int>,

An advanced constructor with explicit Exfiltrator.

source

pub fn add_signal(&self, signal: c_int) -> Result<(), Error>

Registers another signal to the set watched by this Signals instance.

The same restrictions (panics, errors) apply as for the Handle::add_signal method.

source

pub fn pending(&mut self) -> Pending<E>

Returns an iterator of already received signals.

This returns an iterator over all the signal numbers of the signals received since last time they were read (out of the set registered by this Signals instance). Note that they are returned in arbitrary order and a signal instance may returned only once even if it was received multiple times.

This method returns immediately (does not block) and may produce an empty iterator if there are no signals ready.

source

pub fn wait(&mut self) -> Pending<E>

Waits for some signals to be available and returns an iterator.

This is similar to pending. If there are no signals available, it tries to wait for some to arrive. However, due to implementation details, this still can produce an empty iterator.

This can block for arbitrary long time. If the Handle::close method is used in another thread this method will return immediately.

Note that the blocking is done in this method, not in the iterator.

source

pub fn is_closed(&self) -> bool

Is it closed?

See close.

source

pub fn forever(&mut self) -> Forever<'_, E>

Get an infinite iterator over arriving signals.

The iterator’s next() blocks as necessary to wait for signals to arrive. This is adequate if you want to designate a thread solely to handling signals. If multiple signals come at the same time (between two values produced by the iterator), they will be returned in arbitrary order. Multiple instances of the same signal may be collated.

This is also the iterator returned by IntoIterator implementation on &mut Signals.

This iterator terminates only if explicitly closed.

Examples
use signal_hook::consts::signal::*;
use signal_hook::iterator::Signals;

let mut signals = Signals::new(&[SIGUSR1, SIGUSR2])?;
let handle = signals.handle();
thread::spawn(move || {
    for signal in signals.forever() {
        match signal {
            SIGUSR1 => {},
            SIGUSR2 => {},
            _ => unreachable!(),
        }
    }
});
handle.close();
source

pub fn handle(&self) -> Handle

Get a shareable handle to a Handle for this instance.

This can be used to add further signals or close the Signals instance.

Trait Implementations§

source§

impl<E> Debug for SignalsInfo<E>where E: Debug + Exfiltrator, E::Storage: Debug,

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a, E: Exfiltrator> IntoIterator for &'a mut SignalsInfo<E>

§

type Item = <E as Exfiltrator>::Output

The type of the elements being iterated over.
§

type IntoIter = Forever<'a, E>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<E = SignalOnly> !RefUnwindSafe for SignalsInfo<E>

§

impl<E> Send for SignalsInfo<E>

§

impl<E> Sync for SignalsInfo<E>

§

impl<E> Unpin for SignalsInfo<E>

§

impl<E = SignalOnly> !UnwindSafe for SignalsInfo<E>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.