Module signal_hook_mio::v1_0
source · Expand description
A module for integrating signal handling with the MIO 1.0 runtime.
This provides the Signals
struct as an abstraction
which can be used with mio::Poll
.
Examples
use std::io::{Error, ErrorKind};
use signal_hook::consts::signal::*;
use signal_hook_mio::v1_0::Signals;
use mio::{Events, Poll, Interest, Token};
fn main() -> Result<(), Error> {
let mut poll = Poll::new()?;
let mut signals = Signals::new(&[
SIGTERM,
])?;
let signal_token = Token(0);
poll.registry().register(&mut signals, signal_token, Interest::READABLE)?;
let mut events = Events::with_capacity(10);
'outer: loop {
poll.poll(&mut events, None)
.or_else(|e| if e.kind() == ErrorKind::Interrupted {
// We get interrupt when a signal happens inside poll. That's non-fatal, just
// retry.
events.clear();
Ok(())
} else {
Err(e)
})?;
for event in events.iter() {
match event.token() {
Token(0) => {
for signal in signals.pending() {
match signal {
SIGTERM => break 'outer,
_ => unreachable!(),
}
}
},
_ => unreachable!("Register other sources and match for their tokens here"),
}
}
}
Ok(())
}
Structs
- The iterator of one batch of signals.
- A struct which mimics
signal_hook::iterator::SignalsInfo
but also allows usage together with MIO runtime.
Type Aliases
- A simplified signal iterator.