signal: change unix::Signal to return () instead of signum (#1330)

* This simplifies the API surface by returning () instead of the signal
number that was used during registration. This also more closely mirrors
the cross-platform `CtrlC` event stream API
* This is a **breaking change**
This commit is contained in:
Ivan Petkov
2019-07-20 15:12:53 -07:00
committed by GitHub
parent 320a5fdca7
commit d9688bc094
5 changed files with 12 additions and 14 deletions
+3
View File
@@ -3,6 +3,9 @@
### Changed ### Changed
- **Breaking:** `windows::Event` has been removed in favor of `CtrlC` and - **Breaking:** `windows::Event` has been removed in favor of `CtrlC` and
a separate `windows::CtrlBreak` struct. a separate `windows::CtrlBreak` struct.
- **Breaking:** `ctrl_c{,_with_handle}` has been replaced with a `CtrlC` struct
(which can be constructed via `CtrlC::{new, with_handle}`.
- **Breaking:** `unix::Signal` returns `()` instead of the signal number used in registration
# 0.2.9 # 0.2.9
+2 -2
View File
@@ -11,8 +11,8 @@ mod platform {
pub async fn main() { pub async fn main() {
// Create a stream for each of the signals we'd like to handle. // Create a stream for each of the signals we'd like to handle.
let sigint = Signal::new(SIGINT).await.unwrap(); let sigint = Signal::new(SIGINT).await.unwrap().map(|_| SIGINT);
let sigterm = Signal::new(SIGTERM).await.unwrap(); let sigterm = Signal::new(SIGTERM).await.unwrap().map(|_| SIGTERM);
// Use the `select` combinator to merge these two streams into one // Use the `select` combinator to merge these two streams into one
let stream = stream::select(sigint, sigterm); let stream = stream::select(sigint, sigterm);
+2 -6
View File
@@ -20,12 +20,8 @@ mod platform {
// Up until now, we haven't really DONE anything, just prepared // Up until now, we haven't really DONE anything, just prepared
// our futures, now it's time to actually await the results! // our futures, now it's time to actually await the results!
while let Some(the_signal) = stream.next().await { while let Some(_) = stream.next().await {
println!( println!("*Got signal* I should probably reload my config or something");
"*Got signal {:#x}* I should probably reload my config \
or something",
the_signal
);
} }
} }
} }
+3 -4
View File
@@ -254,7 +254,6 @@ impl Driver {
#[derive(Debug)] #[derive(Debug)]
pub struct Signal { pub struct Signal {
driver: Driver, driver: Driver,
signal: c_int,
rx: Receiver<()>, rx: Receiver<()>,
} }
@@ -321,7 +320,7 @@ impl Signal {
let (tx, rx) = channel(1); let (tx, rx) = channel(1);
globals().register_listener(signal as EventId, tx); globals().register_listener(signal as EventId, tx);
Ok(Signal { driver, rx, signal }) Ok(Signal { driver, rx })
}) })
.boxed() .boxed()
} }
@@ -332,12 +331,12 @@ impl Signal {
} }
impl Stream for Signal { impl Stream for Signal {
type Item = c_int; type Item = ();
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let _ = Pin::new(&mut self.driver).poll(cx); let _ = Pin::new(&mut self.driver).poll(cx);
self.rx.poll_recv(cx).map(|item| item.map(|()| self.signal)) self.rx.poll_recv(cx)
} }
} }
+2 -2
View File
@@ -16,8 +16,8 @@ async fn twice() {
for _ in 0..2 { for _ in 0..2 {
send_signal(libc::SIGUSR1); send_signal(libc::SIGUSR1);
let (num, sig) = with_timeout(signal.into_future()).await; let (item, sig) = with_timeout(signal.into_future()).await;
assert_eq!(num, Some(libc::SIGUSR1)); assert_eq!(item, Some(()));
signal = sig; signal = sig;
} }