2016-09-08 17:13:18 -07:00
|
|
|
extern crate futures;
|
|
|
|
|
extern crate tokio_core;
|
|
|
|
|
extern crate tokio_signal;
|
|
|
|
|
|
|
|
|
|
use futures::stream::Stream;
|
|
|
|
|
use tokio_core::reactor::Core;
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2017-06-07 21:20:25 +02:00
|
|
|
// set up a Tokio event loop
|
2016-09-08 17:13:18 -07:00
|
|
|
let mut core = Core::new().unwrap();
|
2017-06-07 21:20:25 +02:00
|
|
|
|
|
|
|
|
// tokio_signal provides a convenience builder for Ctrl+C
|
|
|
|
|
// this even works cross-platform, linux and windows!
|
2016-09-08 17:13:18 -07:00
|
|
|
let ctrlc = tokio_signal::ctrl_c(&core.handle());
|
|
|
|
|
let stream = core.run(ctrlc).unwrap();
|
|
|
|
|
|
2017-06-07 21:18:26 +02:00
|
|
|
println!("This program is now waiting for you to press Ctrl+C");
|
|
|
|
|
|
2017-06-07 21:20:25 +02:00
|
|
|
// Up until now, we haven't really DONE anything, just prepared
|
|
|
|
|
// now it's time to actually schedule, and thus execute, the stream
|
|
|
|
|
// on our event loop
|
2016-09-08 17:13:18 -07:00
|
|
|
core.run(stream.for_each(|()| {
|
|
|
|
|
println!("Ctrl-C received!");
|
|
|
|
|
Ok(())
|
|
|
|
|
})).unwrap();
|
2017-06-07 21:20:51 +02:00
|
|
|
|
|
|
|
|
println!("this won't be printed, because the received Ctrl+C will also kill the program");
|
|
|
|
|
unreachable!();
|
2016-09-08 17:13:18 -07:00
|
|
|
}
|