mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
signal: Ctrl+C example: Defer stream initialisation (and explain how/why)
This commit is contained in:
parent
1c893ef6d3
commit
6a7092b9f7
@ -2,7 +2,7 @@ extern crate futures;
|
|||||||
extern crate tokio_core;
|
extern crate tokio_core;
|
||||||
extern crate tokio_signal;
|
extern crate tokio_signal;
|
||||||
|
|
||||||
use futures::stream::Stream;
|
use futures::{Stream, Future};
|
||||||
use tokio_core::reactor::Core;
|
use tokio_core::reactor::Core;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -10,9 +10,13 @@ fn main() {
|
|||||||
let mut core = Core::new().unwrap();
|
let mut core = Core::new().unwrap();
|
||||||
|
|
||||||
// tokio_signal provides a convenience builder for Ctrl+C
|
// tokio_signal provides a convenience builder for Ctrl+C
|
||||||
// this even works cross-platform, linux and windows!
|
// this even works cross-platform: linux and windows!
|
||||||
let ctrlc = tokio_signal::ctrl_c(&core.handle());
|
//
|
||||||
let stream = core.run(ctrlc).unwrap();
|
// `fn ctrl_c()` produces a `Future` of the actual stream-initialisation
|
||||||
|
// the `flatten_stream()` convenience method lazily defers that
|
||||||
|
// initialisation, allowing us to use it 'as if' it is already the
|
||||||
|
// stream we want, reducing boilerplate Future-handling.
|
||||||
|
let stream = tokio_signal::ctrl_c(&core.handle()).flatten_stream();
|
||||||
|
|
||||||
println!("This program is now waiting for you to press Ctrl+C");
|
println!("This program is now waiting for you to press Ctrl+C");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user