From 4577a8230f28795c55aa61e14eb0fef7ecff249f Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sun, 18 Jul 2021 09:25:45 +0900 Subject: [PATCH 1/2] start to add chapter 12.4 --- Cargo.lock | 15 +++++++++++++-- chapter12/Cargo.toml | 7 ++++++- chapter12/src/12_4/main.rs | 1 + chapter12/src/main.rs | 3 --- 4 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 chapter12/src/12_4/main.rs delete mode 100644 chapter12/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index d25923a..c676255 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,7 @@ name = "chapter12" version = "0.1.0" dependencies = [ "lib", + "signal-hook", ] [[package]] @@ -887,11 +888,21 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "470c5a6397076fae0094aaf06a08e6ba6f37acb77d3b1b91ea92b4d6c8650c39" +dependencies = [ + "libc", + "signal-hook-registry", +] + [[package]] name = "signal-hook-registry" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" +checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0" dependencies = [ "libc", ] diff --git a/chapter12/Cargo.toml b/chapter12/Cargo.toml index e37613d..af0eebe 100644 --- a/chapter12/Cargo.toml +++ b/chapter12/Cargo.toml @@ -7,4 +7,9 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -lib = { path = "../lib" } \ No newline at end of file +lib = { path = "../lib" } +signal-hook = "0.3.9" + +[[bin]] +name = "12_4" +path = "src/12_4/main.rs" \ No newline at end of file diff --git a/chapter12/src/12_4/main.rs b/chapter12/src/12_4/main.rs new file mode 100644 index 0000000..f328e4d --- /dev/null +++ b/chapter12/src/12_4/main.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/chapter12/src/main.rs b/chapter12/src/main.rs deleted file mode 100644 index e7a11a9..0000000 --- a/chapter12/src/main.rs +++ /dev/null @@ -1,3 +0,0 @@ -fn main() { - println!("Hello, world!"); -} From 132609a38db69bafcf8c400c6438aa6a9eb7c8d5 Mon Sep 17 00:00:00 2001 From: yuk1ty Date: Sun, 18 Jul 2021 12:17:26 +0900 Subject: [PATCH 2/2] add 12_4 implementation --- chapter12/Cargo.toml | 5 ++++- chapter12/src/12_4/main.rs | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/chapter12/Cargo.toml b/chapter12/Cargo.toml index af0eebe..79003e9 100644 --- a/chapter12/Cargo.toml +++ b/chapter12/Cargo.toml @@ -8,8 +8,11 @@ edition = "2018" [dependencies] lib = { path = "../lib" } +tokio = { version = "1.8.1", features = ["full"] } signal-hook = "0.3.9" +signal-hook-tokio = { version = "0.3.0", features = ["futures-v0_3"] } +futures = "0.3.15" [[bin]] name = "12_4" -path = "src/12_4/main.rs" \ No newline at end of file +path = "src/12_4/main.rs" diff --git a/chapter12/src/12_4/main.rs b/chapter12/src/12_4/main.rs index f328e4d..55c38ca 100644 --- a/chapter12/src/12_4/main.rs +++ b/chapter12/src/12_4/main.rs @@ -1 +1,25 @@ -fn main() {} +use futures::stream::StreamExt; +use signal_hook::consts::signal::*; +use signal_hook_tokio::Signals; +use std::io::Error; + +#[tokio::main] +async fn main() -> Result<(), Error> { + let signals: Signals = Signals::new(&[SIGINT, SIGTERM])?; + let handle = signals.handle(); + let mut signals = signals.fuse(); + + tokio::spawn(async move { + while let Some(signal) = signals.next().await { + match signal { + SIGINT => println!("SIGINT"), + SIGTERM => println!("SIGTERM"), + _ => unreachable!(), + } + } + }); + + handle.close(); + + Ok(()) +}