Module signal_hook_mio::v0_6
source · [−]Expand description
A module for integrating signal handling with the MIO 0.6 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::v0_6::Signals;
use mio::{Events, Poll, PollOpt, Ready, Token};
fn main() -> Result<(), Error> {
let poll = Poll::new()?;
let mut signals = Signals::new(&[
SIGTERM,
])?;
let signal_token = Token(0);
poll.register(&mut signals, signal_token, Ready::readable(), PollOpt::level())?;
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(0)
} 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 Definitions
A simplified signal iterator.