Skip to content

Commit 33983e2

Browse files
committed
Replace Signal::from_c_int by Signal::try_from
TryFrom wasn't stable when that function was written.
1 parent 882a795 commit 33983e2

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
@@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
## [Unreleased] - ReleaseDate
77
### Added
88
### Changed
9+
- `Signal::from_c_int` has been replaced by `Signal::try_from`
10+
([#1113](https://github.com/nix-rust/nix/pull/1113))
11+
912
- Changed `readlink` and `readlinkat` to return `OsString`
1013
([#1109](https://github.com/nix-rust/nix/pull/1109))
1114

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 {
@@ -413,7 +414,7 @@ impl SigSet {
413414
let mut signum: libc::c_int = unsafe { mem::uninitialized() };
414415
let res = unsafe { libc::sigwait(&self.sigset as *const libc::sigset_t, &mut signum) };
415416

416-
Errno::result(res).map(|_| Signal::from_c_int(signum).unwrap())
417+
Errno::result(res).map(|_| Signal::try_from(signum).unwrap())
417418
}
418419
}
419420

@@ -536,14 +537,15 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigActi
536537
/// # #[macro_use] extern crate lazy_static;
537538
/// # extern crate libc;
538539
/// # extern crate nix;
540+
/// # use std::convert::TryFrom;
539541
/// # use std::sync::atomic::{AtomicBool, Ordering};
540542
/// # use nix::sys::signal::{self, Signal, SigHandler};
541543
/// lazy_static! {
542544
/// static ref SIGNALED: AtomicBool = AtomicBool::new(false);
543545
/// }
544546
///
545547
/// extern fn handle_sigint(signal: libc::c_int) {
546-
/// let signal = Signal::from_c_int(signal).unwrap();
548+
/// let signal = Signal::try_from(signal).unwrap();
547549
/// SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
548550
/// }
549551
///

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)