diff options
author | Carl Lerche <me@carllerche.com> | 2019-10-25 12:50:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-25 12:50:15 -0700 |
commit | 227533d456fe32e48ffcd3796f1e6c8f9318b230 (patch) | |
tree | 498029aaf42dd64eeb8ef0e7d7f29802b45d4e95 /tokio/src/signal/ctrl_c.rs | |
parent | 03a9378297c73c2e56a6d6b55db22b92427b850a (diff) |
net: move into tokio crate (#1683)
A step towards collapsing Tokio sub crates into a single `tokio`
crate (#1318).
The `net` implementation is now provided by the main `tokio` crate.
Functionality can be opted out of by using the various net related
feature flags.
Diffstat (limited to 'tokio/src/signal/ctrl_c.rs')
-rw-r--r-- | tokio/src/signal/ctrl_c.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/tokio/src/signal/ctrl_c.rs b/tokio/src/signal/ctrl_c.rs new file mode 100644 index 00000000..f9dd4679 --- /dev/null +++ b/tokio/src/signal/ctrl_c.rs @@ -0,0 +1,46 @@ +#[cfg(unix)] +use super::unix::{self as os_impl, Signal as Inner}; +#[cfg(windows)] +use super::windows::{self as os_impl, Event as Inner}; + +use futures_core::stream::Stream; +use std::io; +use std::pin::Pin; +use std::task::{Context, Poll}; + +/// Represents a stream which receives "ctrl-c" notifications sent to the process. +/// +/// In general signals are handled very differently across Unix and Windows, but +/// this is somewhat cross platform in terms of how it can be handled. A ctrl-c +/// event to a console process can be represented as a stream for both Windows +/// and Unix. +/// +/// Note that there are a number of caveats listening for signals, and you may +/// wish to read up on the documentation in the `unix` or `windows` module to +/// take a peek. +/// +/// Notably, a notification to this process notifies *all* streams listening to +/// this event. Moreover, the notifications **are coalesced** if they aren't processed +/// quickly enough. This means that if two notifications are received back-to-back, +/// then the stream may only receive one item about the two notifications. +#[must_use = "streams do nothing unless polled"] +#[derive(Debug)] +pub struct CtrlC { + inner: 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 { + type Item = (); + + fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { + Pin::new(&mut self.inner).poll_next(cx) + } +} |