signal: Replace ctrl_c with a CtrlC struct (#1273)

* Add a new `CtrlC` struct which will represent a stream of SIGINT
signals on Unix or the CTRL_C event on Windows
* `CtrlC` implements `Stream<Output = ()>` rather than `IoSteam` as
previously
This commit is contained in:
Ivan Petkov
2019-07-09 08:48:46 -07:00
committed by GitHub
parent 407d15cf93
commit 461eebe612
13 changed files with 129 additions and 116 deletions
+15 -26
View File
@@ -1,7 +1,6 @@
#![deny(warnings, rust_2018_idioms)]
#![feature(async_await)]
use futures_util::future;
use futures_util::stream::StreamExt;
/// how many signals to handle before exiting
@@ -12,11 +11,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// tokio_signal provides a convenience builder for Ctrl+C
// this even works cross-platform: linux and windows!
//
// `fn ctrl_c()` produces a `Future` of the actual stream-initialisation
// `CtrlC::new()` produces a `Future` of the actual stream-initialisation
// so first we await until the signal is ready.
let endless_stream = tokio_signal::ctrl_c().await?;
let endless_stream = tokio_signal::CtrlC::new().await?;
// don't keep going forever: convert the endless stream to a bounded one.
let limited_stream = endless_stream.take(STOP_AFTER);
let mut limited_stream = endless_stream.take(STOP_AFTER);
// how many Ctrl+C have we received so far?
let mut counter = 0;
@@ -28,29 +27,19 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
STOP_AFTER
);
// Stream::for_each is a powerful primitive provided by the Futures crate.
// It turns a Stream into a Future that completes after all stream-items
// have been completed, or the first time the closure returns an error
let future = limited_stream
.map(|result| result.expect("failed to get event"))
.for_each(|()| {
// Note how we manipulate the counter without any fancy synchronisation.
// The borrowchecker realises there can't be any conflicts, so the closure
// can just capture it.
counter += 1;
println!(
"Ctrl+C received {} times! {} more before exit",
counter,
STOP_AFTER - counter
);
// return a result to continue handling the stream
future::ready(())
});
// Up until now, we haven't really DONE anything, just prepared
// now it's time to actually the results!
future.await;
// our futures, now it's time to actually await the results!
while let Some(_) = limited_stream.next().await {
// Note how we manipulate the counter without any fancy synchronisation.
// The borrowchecker realises there can't be any conflicts, so the closure
// can just capture it.
counter += 1;
println!(
"Ctrl+C received {} times! {} more before exit",
counter,
STOP_AFTER - counter
);
}
println!("Stream ended, quiting the program.");
Ok(())
+5 -14
View File
@@ -6,15 +6,13 @@ use std::error::Error;
// A trick to not fail build on non-unix platforms when using unix-specific features.
#[cfg(unix)]
mod platform {
use futures_util::future;
use futures_util::stream::StreamExt;
use std::error::Error;
use tokio_signal::unix::{Signal, SIGHUP};
pub async fn main() -> Result<(), Box<dyn Error>> {
// on Unix, we can listen to whatever signal we want, in this case: SIGHUP
let stream = Signal::new(SIGHUP).await?;
let mut stream = Signal::new(SIGHUP).await?;
println!("Waiting for SIGHUPS (Ctrl+C to quit)");
println!(
@@ -23,22 +21,15 @@ mod platform {
(i.e. this binary)"
);
// for_each is a powerful primitive provided by the Futures crate
// it turns a Stream into a Future that completes after all stream-items
// have been completed.
let future = stream.for_each(|the_signal| {
// Up until now, we haven't really DONE anything, just prepared
// our futures, now it's time to actually await the results!
while let Some(the_signal) = stream.next().await {
println!(
"*Got signal {:#x}* I should probably reload my config \
or something",
the_signal
);
future::ready(())
});
// Up until now, we haven't really DONE anything, just prepared
// now it's time to actually the results!
future.await;
}
Ok(())
}