Skip to content

Commit 6a1c217

Browse files
committed
Replace Signal::from_c_int by Signal::try_from
TryFrom wasn't stable when that function was written.
1 parent 06392c6 commit 6a1c217

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1313
([#1107](https://github.com/nix-rust/nix/pull/1107))
1414

1515
### Changed
16+
- `Signal::from_c_int` has been replaced by `Signal::try_from`
17+
([#1113](https://github.com/nix-rust/nix/pull/1113))
18+
1619
- Changed `readlink` and `readlinkat` to return `OsString`
1720
([#1109](https://github.com/nix-rust/nix/pull/1109))
1821

src/sys/signal.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use libc;
77
use {Error, Result};
88
use errno::Errno;
9+
use std::convert::TryFrom;
910
use std::mem;
1011
use std::fmt;
1112
use std::str::FromStr;
@@ -288,12 +289,12 @@ impl Signal {
288289
pub fn iterator() -> SignalIterator {
289290
SignalIterator{next: 0}
290291
}
292+
}
293+
294+
impl TryFrom<libc::c_int> for Signal {
295+
type Error = Error;
291296

292-
// We do not implement the From trait, because it is supposed to be infallible.
293-
// With Rust RFC 1542 comes the appropriate trait TryFrom. Once it is
294-
// implemented, we'll replace this function.
295-
#[inline]
296-
pub fn from_c_int(signum: libc::c_int) -> Result<Signal> {
297+
fn try_from(signum: libc::c_int) -> Result<Signal> {
297298
if 0 < signum && signum < NSIG {
298299
Ok(unsafe { mem::transmute(signum) })
299300
} else {
@@ -414,7 +415,7 @@ impl SigSet {
414415
let res = unsafe { libc::sigwait(&self.sigset as *const libc::sigset_t, signum.as_mut_ptr()) };
415416

416417
Errno::result(res).map(|_| unsafe {
417-
Signal::from_c_int(signum.assume_init()).unwrap()
418+
Signal::try_from(signum.assume_init()).unwrap()
418419
})
419420
}
420421
}
@@ -542,14 +543,15 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigActi
542543
/// # #[macro_use] extern crate lazy_static;
543544
/// # extern crate libc;
544545
/// # extern crate nix;
546+
/// # use std::convert::TryFrom;
545547
/// # use std::sync::atomic::{AtomicBool, Ordering};
546548
/// # use nix::sys::signal::{self, Signal, SigHandler};
547549
/// lazy_static! {
548550
/// static ref SIGNALED: AtomicBool = AtomicBool::new(false);
549551
/// }
550552
///
551553
/// extern fn handle_sigint(signal: libc::c_int) {
552-
/// let signal = Signal::from_c_int(signal).unwrap();
554+
/// let signal = Signal::try_from(signal).unwrap();
553555
/// SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
554556
/// }
555557
///

src/sys/wait.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use libc::{self, c_int};
22
use Result;
33
use errno::Errno;
4+
use std::convert::TryFrom;
45
use unistd::Pid;
56

67
use sys::signal::Signal;
@@ -126,7 +127,7 @@ fn signaled(status: i32) -> bool {
126127
}
127128

128129
fn term_signal(status: i32) -> Result<Signal> {
129-
Signal::from_c_int(unsafe { libc::WTERMSIG(status) })
130+
Signal::try_from(unsafe { libc::WTERMSIG(status) })
130131
}
131132

132133
fn dumped_core(status: i32) -> bool {
@@ -138,7 +139,7 @@ fn stopped(status: i32) -> bool {
138139
}
139140

140141
fn stop_signal(status: i32) -> Result<Signal> {
141-
Signal::from_c_int(unsafe { libc::WSTOPSIG(status) })
142+
Signal::try_from(unsafe { libc::WSTOPSIG(status) })
142143
}
143144

144145
#[cfg(any(target_os = "android", target_os = "linux"))]

test/sys/test_signal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use libc;
22
use nix::Error;
33
use nix::sys::signal::*;
44
use nix::unistd::*;
5+
use std::convert::TryFrom;
56
use std::sync::atomic::{AtomicBool, Ordering};
67

78
#[test]
@@ -75,7 +76,7 @@ lazy_static! {
7576
}
7677

7778
extern fn test_sigaction_handler(signal: libc::c_int) {
78-
let signal = Signal::from_c_int(signal).unwrap();
79+
let signal = Signal::try_from(signal).unwrap();
7980
SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
8081
}
8182

test/sys/test_signalfd.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryFrom;
2+
13
#[test]
24
fn test_signalfd() {
35
use nix::sys::signalfd::SignalFd;
@@ -20,6 +22,6 @@ fn test_signalfd() {
2022

2123
// And now catch that same signal.
2224
let res = fd.read_signal().unwrap().unwrap();
23-
let signo = Signal::from_c_int(res.ssi_signo as i32).unwrap();
25+
let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
2426
assert_eq!(signo, signal::SIGUSR1);
2527
}

0 commit comments

Comments
 (0)