signal: remove new() constructors in favor of free functions (#1472)

* Also removed any `*_with_handle` related methods in favor of always
using the default reactor
This commit is contained in:
Ivan Petkov
2019-08-18 14:22:09 -07:00
committed by GitHub
parent 7b0c60849c
commit 08b07afbd9
15 changed files with 104 additions and 234 deletions
+8 -19
View File
@@ -1,8 +1,7 @@
#[cfg(unix)]
use super::unix::Signal as Inner;
use super::unix::{self as os_impl, Signal as Inner};
#[cfg(windows)]
use super::windows::Event as Inner;
use crate::driver::Handle;
use super::windows::{self as os_impl, Event as Inner};
use futures_core::stream::Stream;
use std::io;
@@ -30,22 +29,12 @@ pub struct CtrlC {
inner: Inner,
}
impl CtrlC {
/// Creates a new stream which receives "ctrl-c" notifications sent to the
/// process.
///
/// This function binds to the default reactor.
pub fn new() -> io::Result<Self> {
Self::with_handle(&Handle::default())
}
/// Creates a new stream which receives "ctrl-c" notifications sent to the
/// process.
///
/// This function binds to reactor specified by `handle`.
pub fn with_handle(handle: &Handle) -> io::Result<Self> {
Inner::ctrl_c(handle).map(|inner| Self { inner })
}
/// Creates a new stream which receives "ctrl-c" notifications sent to the
/// process.
///
/// This function binds to the default reactor.
pub fn ctrl_c() -> io::Result<CtrlC> {
os_impl::ctrl_c().map(|inner| CtrlC { inner })
}
impl Stream for CtrlC {
+6 -6
View File
@@ -29,7 +29,7 @@
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create an infinite stream of "Ctrl+C" notifications. Each item received
//! // on this stream may represent multiple ctrl-c signals.
//! let ctrl_c = signal::CtrlC::new()?;
//! let ctrl_c = signal::ctrl_c()?;
//!
//! // Process each ctrl-c as it comes in
//! let prog = ctrl_c.for_each(|_| {
@@ -49,7 +49,7 @@
//! #![feature(async_await)]
//! # #[cfg(unix)] {
//!
//! use tokio_net::signal::{self, unix::{Signal, SignalKind}};
//! use tokio_net::signal::{self, unix::{signal, SignalKind}};
//!
//! use futures_util::future;
//! use futures_util::stream::StreamExt;
@@ -58,7 +58,7 @@
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create an infinite stream of "Ctrl+C" notifications. Each item received
//! // on this stream may represent multiple ctrl-c signals.
//! let ctrl_c = signal::CtrlC::new()?;
//! let ctrl_c = signal::ctrl_c()?;
//!
//! // Process each ctrl-c as it comes in
//! let prog = ctrl_c.for_each(|_| {
@@ -70,10 +70,10 @@
//!
//! // Like the previous example, this is an infinite stream of signals
//! // being received, and signals may be coalesced while pending.
//! let stream = Signal::new(SignalKind::hangup())?;
//! let stream = signal(SignalKind::hangup())?;
//!
//! // Convert out stream into a future and block the program
//! let (signal, _signal) = stream.into_future().await;
//! let (signal, _stream) = stream.into_future().await;
//! println!("got signal {:?}", signal);
//! Ok(())
//! }
@@ -93,4 +93,4 @@ mod os {
pub mod unix;
pub mod windows;
pub use self::ctrl_c::CtrlC;
pub use self::ctrl_c::{ctrl_c, CtrlC};
+39 -62
View File
@@ -6,7 +6,6 @@
#![cfg(unix)]
use super::registry::{globals, EventId, EventInfo, Globals, Init, Storage};
use crate::driver::Handle;
use crate::util::PollEvented;
use tokio_io::AsyncRead;
@@ -280,7 +279,7 @@ impl Future for Driver {
}
impl Driver {
fn new(handle: &Handle) -> io::Result<Driver> {
fn new() -> io::Result<Driver> {
// NB: We give each driver a "fresh" reciever file descriptor to avoid
// the issues described in alexcrichton/tokio-process#42.
//
@@ -295,7 +294,7 @@ impl Driver {
// either, since we can't compare Handles or assume they will always
// point to the exact same reactor.
let stream = globals().receiver.try_clone()?;
let wakeup = PollEvented::new_with_handle(stream, handle)?;
let wakeup = PollEvented::new(stream);
Ok(Driver { wakeup })
}
@@ -359,70 +358,48 @@ pub struct Signal {
rx: Receiver<()>,
}
impl Signal {
/// Creates a new stream which will receive notifications when the current
/// process receives the signal `signal`.
///
/// This function will create a new stream which binds to the default reactor.
/// The `Signal` stream is an infinite stream which will receive
/// notifications whenever a signal is received. More documentation can be
/// found on `Signal` itself, but to reiterate:
///
/// * Signals may be coalesced beyond what the kernel already does.
/// * Once a signal handler is registered with the process the underlying
/// libc signal handler is never unregistered.
///
/// A `Signal` stream can be created for a particular signal number
/// multiple times. When a signal is received then all the associated
/// channels will receive the signal notification.
///
/// # Errors
///
/// * If the lower-level C functions fail for some reason.
/// * If the previous initialization of this specific signal failed.
/// * If the signal is one of
/// [`signal_hook::FORBIDDEN`](https://docs.rs/signal-hook/*/signal_hook/fn.register.html#panics)
pub fn new(kind: SignalKind) -> io::Result<Self> {
Signal::with_handle(kind, &Handle::default())
}
/// Creates a new stream which will receive notifications when the current
/// process receives the signal `signal`.
///
/// This function will create a new stream which binds to the default reactor.
/// The `Signal` stream is an infinite stream which will receive
/// notifications whenever a signal is received. More documentation can be
/// found on `Signal` itself, but to reiterate:
///
/// * Signals may be coalesced beyond what the kernel already does.
/// * Once a signal handler is registered with the process the underlying
/// libc signal handler is never unregistered.
///
/// A `Signal` stream can be created for a particular signal number
/// multiple times. When a signal is received then all the associated
/// channels will receive the signal notification.
///
/// # Errors
///
/// * If the lower-level C functions fail for some reason.
/// * If the previous initialization of this specific signal failed.
/// * If the signal is one of
/// [`signal_hook::FORBIDDEN`](https://docs.rs/signal-hook/*/signal_hook/fn.register.html#panics)
pub fn signal(kind: SignalKind) -> io::Result<Signal> {
let signal = kind.0;
/// Creates a new stream which will receive notifications when the current
/// process receives the signal `signal`.
///
/// This function will create a new stream which may be based on the
/// provided reactor handle.
/// The `Signal` stream is an infinite stream which will receive
/// notifications whenever a signal is received. More documentation can be
/// found on `Signal` itself, but to reiterate:
///
/// * Signals may be coalesced beyond what the kernel already does.
/// * Once a signal handler is registered with the process the underlying
/// libc signal handler is never unregistered.
///
/// A `Signal` stream can be created for a particular signal number
/// multiple times. When a signal is received then all the associated
/// channels will receive the signal notification.
pub fn with_handle(kind: SignalKind, handle: &Handle) -> io::Result<Self> {
let signal = kind.0;
// Turn the signal delivery on once we are ready for it
signal_enable(signal)?;
// Turn the signal delivery on once we are ready for it
signal_enable(signal)?;
// Ensure there's a driver for our associated event loop processing
// signals.
let driver = Driver::new()?;
// Ensure there's a driver for our associated event loop processing
// signals.
let driver = Driver::new(&handle)?;
// One wakeup in a queue is enough, no need for us to buffer up any
// more.
let (tx, rx) = channel(1);
globals().register_listener(signal as EventId, tx);
// One wakeup in a queue is enough, no need for us to buffer up any
// more.
let (tx, rx) = channel(1);
globals().register_listener(signal as EventId, tx);
Ok(Signal { driver, rx })
}
Ok(Signal { driver, rx })
}
pub(crate) fn ctrl_c(handle: &Handle) -> io::Result<Self> {
Self::with_handle(SignalKind::interrupt(), handle)
}
pub(crate) fn ctrl_c() -> io::Result<Signal> {
signal(SignalKind::interrupt())
}
impl Stream for Signal {
+13 -36
View File
@@ -8,7 +8,6 @@
#![cfg(windows)]
use super::registry::{globals, EventId, EventInfo, Init, Storage};
use crate::driver::Handle;
use tokio_sync::mpsc::{channel, Receiver};
@@ -85,23 +84,7 @@ pub(crate) struct Event {
}
impl Event {
/// Creates a new stream listening for the `CTRL_C_EVENT` events.
///
/// This function will register a handler via `SetConsoleCtrlHandler` and
/// deliver notifications to the returned stream.
pub(crate) fn ctrl_c(handle: &Handle) -> io::Result<Self> {
Event::new(CTRL_C_EVENT, handle)
}
/// Creates a new stream listening for the `CTRL_BREAK_EVENT` events.
///
/// This function will register a handler via `SetConsoleCtrlHandler` and
/// deliver notifications to the returned stream.
fn ctrl_break_handle(handle: &Handle) -> io::Result<Self> {
Event::new(CTRL_BREAK_EVENT, handle)
}
fn new(signum: DWORD, _handle: &Handle) -> io::Result<Self> {
fn new(signum: DWORD) -> io::Result<Self> {
global_init()?;
let (tx, rx) = channel(1);
@@ -111,6 +94,10 @@ impl Event {
}
}
pub(crate) fn ctrl_c() -> io::Result<Event> {
Event::new(CTRL_C_EVENT)
}
impl Stream for Event {
type Item = ();
@@ -167,22 +154,12 @@ pub struct CtrlBreak {
inner: Event,
}
impl CtrlBreak {
/// Creates a new stream which receives "ctrl-break" notifications sent to the
/// process.
///
/// This function binds to the default reactor.
pub fn new() -> io::Result<Self> {
Self::with_handle(&Handle::default())
}
/// Creates a new stream which receives "ctrl-break" notifications sent to the
/// process.
///
/// This function binds to reactor specified by `handle`.
pub fn with_handle(handle: &Handle) -> io::Result<Self> {
Event::ctrl_break_handle(handle).map(|inner| Self { inner })
}
/// Creates a new stream which receives "ctrl-break" notifications sent to the
/// process.
///
/// This function binds to the default reactor.
pub fn ctrl_break() -> io::Result<CtrlBreak> {
Event::new(CTRL_BREAK_EVENT).map(|inner| CtrlBreak { inner })
}
impl Stream for CtrlBreak {
@@ -212,7 +189,7 @@ mod tests {
#[tokio::test]
async fn ctrl_c() {
let ctrl_c = crate::signal::CtrlC::new().expect("failed to create CtrlC");
let ctrl_c = crate::signal::ctrl_c().expect("failed to create CtrlC");
// Windows doesn't have a good programmatic way of sending events
// like sending signals on Unix, so we'll stub out the actual OS
@@ -226,7 +203,7 @@ mod tests {
#[tokio::test]
async fn ctrl_break() {
let ctrl_break = super::CtrlBreak::new().expect("failed to create CtrlC");
let ctrl_break = super::ctrl_break().expect("failed to create CtrlC");
// Windows doesn't have a good programmatic way of sending events
// like sending signals on Unix, so we'll stub out the actual OS