From df4e3f7b5a5e397a82a47acf2ac23ee261c248e4 Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Tue, 19 Jan 2021 19:10:17 -0800 Subject: [PATCH] Update signal-hook-tokio for tokio v1.0 This drops support for pre-1.0 versions of tokio, and removes the per-version features and modules. --- Cargo.lock | 384 +----------------- signal-hook-tokio/Cargo.toml | 12 +- signal-hook-tokio/src/lib.rs | 243 ++++------- .../tests/{tokio_0_3.rs => tests.rs} | 7 +- signal-hook-tokio/tests/tokio_0_1.rs | 68 ---- 5 files changed, 90 insertions(+), 624 deletions(-) rename signal-hook-tokio/tests/{tokio_0_3.rs => tests.rs} (91%) delete mode 100644 signal-hook-tokio/tests/tokio_0_1.rs diff --git a/Cargo.lock b/Cargo.lock index d0ac113..fb950c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -89,7 +89,7 @@ dependencies = [ "async-io", "async-mutex", "blocking", - "crossbeam-utils 0.8.1", + "crossbeam-utils", "futures-channel", "futures-core", "futures-io", @@ -150,27 +150,11 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" -[[package]] -name = "byteorder" -version = "1.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" - [[package]] name = "bytes" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" -dependencies = [ - "byteorder", - "iovec", -] - -[[package]] -name = "bytes" -version = "0.6.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0dcbc35f504eb6fc275a6d20e4ebcda18cf50d40ba6fabff8c711fa16cb3b16" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" [[package]] name = "cache-padded" @@ -196,15 +180,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "cloudabi" -version = "0.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" -dependencies = [ - "bitflags", -] - [[package]] name = "concurrent-queue" version = "1.2.2" @@ -214,54 +189,6 @@ dependencies = [ "cache-padded", ] -[[package]] -name = "crossbeam-deque" -version = "0.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" -dependencies = [ - "crossbeam-epoch", - "crossbeam-utils 0.7.2", - "maybe-uninit", -] - -[[package]] -name = "crossbeam-epoch" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" -dependencies = [ - "autocfg", - "cfg-if 0.1.10", - "crossbeam-utils 0.7.2", - "lazy_static", - "maybe-uninit", - "memoffset", - "scopeguard", -] - -[[package]] -name = "crossbeam-queue" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" -dependencies = [ - "cfg-if 0.1.10", - "crossbeam-utils 0.7.2", - "maybe-uninit", -] - -[[package]] -name = "crossbeam-utils" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" -dependencies = [ - "autocfg", - "cfg-if 0.1.10", - "lazy_static", -] - [[package]] name = "crossbeam-utils" version = "0.8.1" @@ -288,12 +215,6 @@ dependencies = [ "instant", ] -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -310,12 +231,6 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" -[[package]] -name = "futures" -version = "0.1.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c7e4c2612746b0df8fed4ce0c69156021b704c9aefa360311c04e6e9e002eed" - [[package]] name = "futures" version = "0.3.8" @@ -506,15 +421,6 @@ version = "0.2.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" -[[package]] -name = "lock_api" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" -dependencies = [ - "scopeguard", -] - [[package]] name = "lock_api" version = "0.4.2" @@ -533,27 +439,12 @@ dependencies = [ "cfg-if 0.1.10", ] -[[package]] -name = "maybe-uninit" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" - [[package]] name = "memchr" version = "2.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" -[[package]] -name = "memoffset" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "043175f069eda7b85febe4a74abbaeff828d9f8b448515d3151a14a3542811aa" -dependencies = [ - "autocfg", -] - [[package]] name = "mio" version = "0.6.23" @@ -671,17 +562,6 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" -[[package]] -name = "parking_lot" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" -dependencies = [ - "lock_api 0.3.4", - "parking_lot_core 0.6.2", - "rustc_version", -] - [[package]] name = "parking_lot" version = "0.11.1" @@ -689,23 +569,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" dependencies = [ "instant", - "lock_api 0.4.2", - "parking_lot_core 0.8.2", -] - -[[package]] -name = "parking_lot_core" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" -dependencies = [ - "cfg-if 0.1.10", - "cloudabi", - "libc", - "redox_syscall", - "rustc_version", - "smallvec 0.6.13", - "winapi 0.3.9", + "lock_api", + "parking_lot_core", ] [[package]] @@ -718,7 +583,7 @@ dependencies = [ "instant", "libc", "redox_syscall", - "smallvec 1.6.0", + "smallvec", "winapi 0.3.9", ] @@ -803,36 +668,12 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver", -] - [[package]] name = "scopeguard" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" -[[package]] -name = "semver" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" -dependencies = [ - "semver-parser", -] - -[[package]] -name = "semver-parser" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" - [[package]] name = "serial_test" version = "0.5.1" @@ -840,7 +681,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0bccbcf40c8938196944a3da0e133e031a33f4d6b72db3bda3cc556e361905d" dependencies = [ "lazy_static", - "parking_lot 0.11.1", + "parking_lot", "serial_test_derive", ] @@ -908,13 +749,11 @@ dependencies = [ name = "signal-hook-tokio" version = "0.2.0" dependencies = [ - "futures 0.1.30", - "futures 0.3.8", + "futures", "libc", "serial_test", "signal-hook", - "tokio 0.1.22", - "tokio 0.3.6", + "tokio", ] [[package]] @@ -923,15 +762,6 @@ version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" -[[package]] -name = "smallvec" -version = "0.6.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" -dependencies = [ - "maybe-uninit", -] - [[package]] name = "smallvec" version = "1.6.0" @@ -962,219 +792,35 @@ dependencies = [ [[package]] name = "tokio" -version = "0.1.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" -dependencies = [ - "bytes 0.4.12", - "futures 0.1.30", - "mio 0.6.23", - "num_cpus", - "tokio-codec", - "tokio-current-thread", - "tokio-executor", - "tokio-fs", - "tokio-io", - "tokio-reactor", - "tokio-sync", - "tokio-tcp", - "tokio-threadpool", - "tokio-timer", - "tokio-udp", - "tokio-uds", -] - -[[package]] -name = "tokio" -version = "0.3.6" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "720ba21c25078711bf456d607987d95bce90f7c3bea5abe1db587862e7a1e87c" +checksum = "d258221f566b6c803c7b4714abadc080172b272090cdc5e244a6d4dd13c3a6bd" dependencies = [ "autocfg", - "bytes 0.6.0", - "futures-core", + "bytes", "libc", "memchr", "mio 0.7.7", "num_cpus", "once_cell", - "parking_lot 0.11.1", + "parking_lot", "pin-project-lite", "signal-hook-registry 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "slab", "tokio-macros", "winapi 0.3.9", ] -[[package]] -name = "tokio-codec" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25b2998660ba0e70d18684de5d06b70b70a3a747469af9dea7618cc59e75976b" -dependencies = [ - "bytes 0.4.12", - "futures 0.1.30", - "tokio-io", -] - -[[package]] -name = "tokio-current-thread" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1de0e32a83f131e002238d7ccde18211c0a5397f60cbfffcb112868c2e0e20e" -dependencies = [ - "futures 0.1.30", - "tokio-executor", -] - -[[package]] -name = "tokio-executor" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb2d1b8f4548dbf5e1f7818512e9c406860678f29c300cdf0ebac72d1a3a1671" -dependencies = [ - "crossbeam-utils 0.7.2", - "futures 0.1.30", -] - -[[package]] -name = "tokio-fs" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "297a1206e0ca6302a0eed35b700d292b275256f596e2f3fea7729d5e629b6ff4" -dependencies = [ - "futures 0.1.30", - "tokio-io", - "tokio-threadpool", -] - -[[package]] -name = "tokio-io" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57fc868aae093479e3131e3d165c93b1c7474109d13c90ec0dda2a1bbfff0674" -dependencies = [ - "bytes 0.4.12", - "futures 0.1.30", - "log", -] - [[package]] name = "tokio-macros" -version = "0.3.2" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46dfffa59fc3c8aad216ed61bdc2c263d2b9d87a9c8ac9de0c11a813e51b6db7" +checksum = "42517d2975ca3114b22a16192634e8241dc5cc1f130be194645970cc1c371494" dependencies = [ "proc-macro2", "quote", "syn", ] -[[package]] -name = "tokio-reactor" -version = "0.1.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09bc590ec4ba8ba87652da2068d150dcada2cfa2e07faae270a5e0409aa51351" -dependencies = [ - "crossbeam-utils 0.7.2", - "futures 0.1.30", - "lazy_static", - "log", - "mio 0.6.23", - "num_cpus", - "parking_lot 0.9.0", - "slab", - "tokio-executor", - "tokio-io", - "tokio-sync", -] - -[[package]] -name = "tokio-sync" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "edfe50152bc8164fcc456dab7891fa9bf8beaf01c5ee7e1dd43a397c3cf87dee" -dependencies = [ - "fnv", - "futures 0.1.30", -] - -[[package]] -name = "tokio-tcp" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98df18ed66e3b72e742f185882a9e201892407957e45fbff8da17ae7a7c51f72" -dependencies = [ - "bytes 0.4.12", - "futures 0.1.30", - "iovec", - "mio 0.6.23", - "tokio-io", - "tokio-reactor", -] - -[[package]] -name = "tokio-threadpool" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df720b6581784c118f0eb4310796b12b1d242a7eb95f716a8367855325c25f89" -dependencies = [ - "crossbeam-deque", - "crossbeam-queue", - "crossbeam-utils 0.7.2", - "futures 0.1.30", - "lazy_static", - "log", - "num_cpus", - "slab", - "tokio-executor", -] - -[[package]] -name = "tokio-timer" -version = "0.2.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93044f2d313c95ff1cb7809ce9a7a05735b012288a888b62d4434fd58c94f296" -dependencies = [ - "crossbeam-utils 0.7.2", - "futures 0.1.30", - "slab", - "tokio-executor", -] - -[[package]] -name = "tokio-udp" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2a0b10e610b39c38b031a2fcab08e4b82f16ece36504988dcbd81dbba650d82" -dependencies = [ - "bytes 0.4.12", - "futures 0.1.30", - "log", - "mio 0.6.23", - "tokio-codec", - "tokio-io", - "tokio-reactor", -] - -[[package]] -name = "tokio-uds" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab57a4ac4111c8c9dbcf70779f6fc8bc35ae4b2454809febac840ad19bd7e4e0" -dependencies = [ - "bytes 0.4.12", - "futures 0.1.30", - "iovec", - "libc", - "log", - "mio 0.6.23", - "mio-uds", - "tokio-codec", - "tokio-io", - "tokio-reactor", -] - [[package]] name = "unicode-xid" version = "0.2.1" diff --git a/signal-hook-tokio/Cargo.toml b/signal-hook-tokio/Cargo.toml index b49463b..34f382b 100644 --- a/signal-hook-tokio/Cargo.toml +++ b/signal-hook-tokio/Cargo.toml @@ -19,20 +19,14 @@ edition = "2018" travis-ci = { repository = "vorner/signal-hook" } maintenance = { status = "actively-developed" } -[features] -support-v0_1 = ["tokio-0_1", "futures-0_1"] -support-v0_3 = ["tokio-0_3", "futures-0_3"] - [dependencies] libc = "~0.2" signal-hook = { version = "~0.3", path = ".." } -tokio-0_1 = { package = "tokio", version = "~0.1", optional = true } -futures-0_1 = { package = "futures", version = "~0.1", optional = true } -tokio-0_3 = { package = "tokio", version = "~0.3", features = ["net"], optional = true } -futures-0_3 = { package = "futures", version = "~0.3", optional = true } +futures = "~0.3" +tokio = { version = "~1.0", features = ["net"] } [dev-dependencies] -tokio-0_3 = { package = "tokio", version = "~0.3", features = ["full"]} +tokio = { package = "tokio", version = "~1.0", features = ["full"] } serial_test = "~0.5" [package.metadata.docs.rs] diff --git a/signal-hook-tokio/src/lib.rs b/signal-hook-tokio/src/lib.rs index 4327dd1..2ecfd15 100644 --- a/signal-hook-tokio/src/lib.rs +++ b/signal-hook-tokio/src/lib.rs @@ -4,16 +4,57 @@ //! A crate for integrating signal handling with the Tokio runtime. //! -//! There are different sub modules for supporting different Tokio -//! versions. The support for a version must be activated by a -//! feature flag: +//! This provides the [`Signals`][Signals] struct which acts as a +//! [`Stream`][`futures::stream::Stream`] of signals. //! -//! * `support-v0_1` for sub module [`v0_1`] -//! * `support-v0_3` for sub module [`v0_3`] +//! # Example //! -//! See the specific sub modules for usage examples. +//! ```rust +//! use std::io::Error; +//! +//! use signal_hook::consts::signal::*; +//! use signal_hook_tokio::Signals; +//! +//! use futures::stream::StreamExt; +//! +//! async fn handle_signals(signals: Signals) { +//! let mut signals = signals.fuse(); +//! while let Some(signal) = signals.next().await { +//! match signal { +//! SIGHUP => { +//! // Reload configuration +//! // Reopen the log file +//! } +//! SIGTERM | SIGINT | SIGQUIT => { +//! // Shutdown the system; +//! }, +//! _ => unreachable!(), +//! } +//! } +//! } +//! +//! #[tokio::main] +//! async fn main() -> Result<(), Error> { +//! let signals = Signals::new(&[ +//! SIGHUP, +//! SIGTERM, +//! SIGINT, +//! SIGQUIT, +//! ])?; +//! let handle = signals.handle(); +//! +//! let signals_task = tokio::spawn(handle_signals(signals)); +//! +//! // Execute your main program logic +//! +//! // Terminate the signal stream. +//! handle.close(); +//! signals_task.await?; +//! +//! Ok(()) +//! } +//! ``` -#[cfg(any(feature = "support-v0_1", feature = "support-v0_3"))] macro_rules! implement_signals_with_pipe { ($pipe:ty) => { use std::borrow::Borrow; @@ -73,179 +114,37 @@ macro_rules! implement_signals_with_pipe { }; } -/// A module for integrating signal handling with the Tokio 0.1 runtime. -/// -/// This provides the [`Signals`][v0_1::Signals] struct which acts as a -/// [`Stream`][`tokio_0_1::prelude::Stream`] of signals. -/// -/// # Example -/// -/// ```rust -/// # use tokio_0_1 as tokio; -/// use std::io::Error; -/// -/// use signal_hook::consts::signal::*; -/// use signal_hook_tokio::v0_1::Signals; -/// use tokio::prelude::*; -/// -/// enum SignalResult { -/// Err(Error), -/// Shutdown, -/// } -/// -/// fn main() -> Result<(), Error> { -/// let signals = Signals::new(&[ -/// SIGTERM, -/// # SIGUSR1, -/// ])? -/// .map_err(|error| { -/// SignalResult::Err(error) -/// }) -/// .for_each(|sig| { -/// // Return an error to stop the for_each iteration. -/// match sig { -/// SIGTERM => return future::err(SignalResult::Shutdown), -/// # SIGUSR1 => return future::err(SignalResult::Shutdown), -/// _ => unreachable!(), -/// } -/// }) -/// .map_err(|reason| { -/// if let SignalResult::Err(error) = reason { -/// eprintln!("Internal error: {}", error); -/// } -/// }); -/// -/// # signal_hook::low_level::raise(SIGUSR1).unwrap(); -/// tokio::run(signals); -/// Ok(()) -/// } -/// ``` -#[cfg(feature = "support-v0_1")] -pub mod v0_1 { - use futures_0_1::stream::Stream; - use futures_0_1::{Async, Poll}; - use signal_hook::iterator::backend::PollResult; - use tokio_0_1::io::AsyncRead; - use tokio_0_1::net::unix::UnixStream; +use std::pin::Pin; - implement_signals_with_pipe!(UnixStream); +use futures::stream::Stream; +use futures::task::{Context, Poll}; +use signal_hook::iterator::backend::PollResult; +use tokio::io::{AsyncRead, ReadBuf}; +use tokio::net::UnixStream; - impl SignalsInfo { - /// Check if signals arrived by polling the stream for some bytes. - /// - /// Returns true if it was possible to read a byte and false otherwise. - fn has_signals(read: &mut UnixStream) -> Result { - match read.poll_read(&mut [0u8]) { - Poll::Ok(Async::NotReady) => Ok(false), - Poll::Ok(Async::Ready(num_read)) => Ok(num_read > 0), - Poll::Err(error) => Err(error), - } - } - } +implement_signals_with_pipe!(UnixStream); - impl Stream for SignalsInfo { - type Item = E::Output; - type Error = Error; - fn poll(&mut self) -> Poll, Self::Error> { - match self.0.poll_signal(&mut SignalsInfo::::has_signals) { - PollResult::Pending => Poll::Ok(Async::NotReady), - PollResult::Signal(result) => Poll::Ok(Async::Ready(Some(result))), - PollResult::Closed => Poll::Ok(Async::Ready(None)), - PollResult::Err(error) => Poll::Err(error), - } +impl Signals { + fn has_signals(read: &mut UnixStream, ctx: &mut Context<'_>) -> Result { + let mut buf = [0u8]; + let mut read_buf = ReadBuf::new(&mut buf); + match Pin::new(read).poll_read(ctx, &mut read_buf) { + Poll::Pending => Ok(false), + Poll::Ready(Ok(())) => Ok(true), + Poll::Ready(Err(error)) => Err(error), } } } -/// A module for integrating signal handling with the Tokio 0.3 runtime. -/// -/// This provides the [`Signals`][v0_3::Signals] struct which acts as a -/// [`Stream`][`futures_0_3::stream::Stream`] of signals. -/// -/// # Example -/// -/// ```rust -/// # use tokio_0_3 as tokio; -/// # use futures_0_3 as futures; -/// -/// use std::io::Error; -/// -/// use signal_hook::consts::signal::*; -/// use signal_hook_tokio::v0_3::Signals; -/// -/// use futures::stream::StreamExt; -/// -/// async fn handle_signals(signals: Signals) { -/// let mut signals = signals.fuse(); -/// while let Some(signal) = signals.next().await { -/// match signal { -/// SIGHUP => { -/// // Reload configuration -/// // Reopen the log file -/// } -/// SIGTERM | SIGINT | SIGQUIT => { -/// // Shutdown the system; -/// }, -/// _ => unreachable!(), -/// } -/// } -/// } -/// -/// #[tokio::main] -/// async fn main() -> Result<(), Error> { -/// let signals = Signals::new(&[ -/// SIGHUP, -/// SIGTERM, -/// SIGINT, -/// SIGQUIT, -/// ])?; -/// let handle = signals.handle(); -/// -/// let signals_task = tokio::spawn(handle_signals(signals)); -/// -/// // Execute your main program logic -/// -/// // Terminate the signal stream. -/// handle.close(); -/// signals_task.await?; -/// -/// Ok(()) -/// } -/// ``` -#[cfg(feature = "support-v0_3")] -pub mod v0_3 { - use std::pin::Pin; - - use futures_0_3::stream::Stream; - use futures_0_3::task::{Context, Poll}; - use signal_hook::iterator::backend::PollResult; - use tokio_0_3::io::{AsyncRead, ReadBuf}; - use tokio_0_3::net::UnixStream; - - implement_signals_with_pipe!(UnixStream); - - impl Signals { - fn has_signals(read: &mut UnixStream, ctx: &mut Context<'_>) -> Result { - let mut buf = [0u8]; - let mut read_buf = ReadBuf::new(&mut buf); - match Pin::new(read).poll_read(ctx, &mut read_buf) { - Poll::Pending => Ok(false), - Poll::Ready(Ok(())) => Ok(true), - Poll::Ready(Err(error)) => Err(error), - } - } - } +impl Stream for Signals { + type Item = c_int; - impl Stream for Signals { - type Item = c_int; - - fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll> { - match self.0.poll_signal(&mut |read| Self::has_signals(read, ctx)) { - PollResult::Signal(sig) => Poll::Ready(Some(sig)), - PollResult::Closed => Poll::Ready(None), - PollResult::Pending => Poll::Pending, - PollResult::Err(error) => panic!("Unexpected error: {}", error), - } + fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll> { + match self.0.poll_signal(&mut |read| Self::has_signals(read, ctx)) { + PollResult::Signal(sig) => Poll::Ready(Some(sig)), + PollResult::Closed => Poll::Ready(None), + PollResult::Pending => Poll::Pending, + PollResult::Err(error) => panic!("Unexpected error: {}", error), } } } diff --git a/signal-hook-tokio/tests/tokio_0_3.rs b/signal-hook-tokio/tests/tests.rs similarity index 91% rename from signal-hook-tokio/tests/tokio_0_3.rs rename to signal-hook-tokio/tests/tests.rs index 9dd8370..be26260 100644 --- a/signal-hook-tokio/tests/tokio_0_3.rs +++ b/signal-hook-tokio/tests/tests.rs @@ -1,8 +1,3 @@ -#![cfg(feature = "support-v0_3")] - -use futures_0_3 as futures; -use tokio_0_3 as tokio; - use futures::stream::StreamExt; use std::sync::atomic::{AtomicBool, Ordering}; @@ -11,7 +6,7 @@ use std::time::Duration; use signal_hook::consts::SIGUSR1; use signal_hook::low_level::raise; -use signal_hook_tokio::v0_3::Signals; +use signal_hook_tokio::Signals; use serial_test::serial; diff --git a/signal-hook-tokio/tests/tokio_0_1.rs b/signal-hook-tokio/tests/tokio_0_1.rs deleted file mode 100644 index 038bc8c..0000000 --- a/signal-hook-tokio/tests/tokio_0_1.rs +++ /dev/null @@ -1,68 +0,0 @@ -#![cfg(feature = "support-v0_1")] - -use std::sync::atomic::{AtomicUsize, Ordering}; -use std::sync::Arc; -use std::time::{Duration, Instant}; - -use signal_hook::consts::{SIGUSR1, SIGUSR2}; -use signal_hook::low_level::raise; -use signal_hook_tokio::v0_1::Signals; -use tokio_0_1::prelude::*; -use tokio_0_1::timer::Interval; - -use serial_test::serial; - -#[test] -#[serial] -fn repeated() { - let signals = Signals::new(&[SIGUSR1]) - .unwrap() - .take(20) - .map_err(|e| panic!("{}", e)) - .for_each(|sig| { - assert_eq!(sig, SIGUSR1); - raise(SIGUSR1).unwrap(); - Ok(()) - }); - raise(SIGUSR1).unwrap(); - tokio_0_1::run(signals); -} - -/// A test where we actually wait for something ‒ the stream/reactor goes to sleep. -#[test] -#[serial] -fn delayed() { - const CNT: usize = 10; - let cnt = Arc::new(AtomicUsize::new(0)); - let inc_cnt = Arc::clone(&cnt); - let signals = Signals::new(&[SIGUSR1, SIGUSR2]) - .unwrap() - .filter(|sig| *sig == SIGUSR2) - .take(CNT as u64) - .map_err(|e| panic!("{}", e)) - .for_each(move |_| { - inc_cnt.fetch_add(1, Ordering::Relaxed); - Ok(()) - }); - let senders = Interval::new(Instant::now(), Duration::from_millis(250)) - .map_err(|e| panic!("{}", e)) - .for_each(|_| { - raise(SIGUSR2).unwrap(); - Ok(()) - }); - let both = signals.select(senders).map(|_| ()).map_err(|_| ()); - tokio_0_1::run(both); - // Just make sure it didn't terminate prematurely - assert_eq!(CNT, cnt.load(Ordering::Relaxed)); -} - -#[test] -#[serial] -fn close_signal_stream() { - let mut signals = Signals::new(&[SIGUSR1, SIGUSR2]).unwrap(); - signals.handle().close(); - - let async_result = signals.poll().unwrap(); - - assert_eq!(async_result, Async::Ready(None)); -}