Skip to content

Commit

Permalink
Replace Signal::from_c_int by Signal::try_from
Browse files Browse the repository at this point in the history
TryFrom wasn't stable when that function was written.
  • Loading branch information
asomers committed Aug 30, 2019
1 parent 882a795 commit 33983e2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased] - ReleaseDate
### Added
### Changed
- `Signal::from_c_int` has been replaced by `Signal::try_from`
([#1113](https://github.com/nix-rust/nix/pull/1113))

- Changed `readlink` and `readlinkat` to return `OsString`
([#1109](https://github.com/nix-rust/nix/pull/1109))

Expand Down
16 changes: 9 additions & 7 deletions src/sys/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use libc;
use {Error, Result};
use errno::Errno;
use std::convert::TryFrom;
use std::mem;
use std::fmt;
use std::str::FromStr;
Expand Down Expand Up @@ -288,12 +289,12 @@ impl Signal {
pub fn iterator() -> SignalIterator {
SignalIterator{next: 0}
}
}

impl TryFrom<libc::c_int> for Signal {
type Error = Error;

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

Errno::result(res).map(|_| Signal::from_c_int(signum).unwrap())
Errno::result(res).map(|_| Signal::try_from(signum).unwrap())
}
}

Expand Down Expand Up @@ -536,14 +537,15 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigActi
/// # #[macro_use] extern crate lazy_static;
/// # extern crate libc;
/// # extern crate nix;
/// # use std::convert::TryFrom;
/// # use std::sync::atomic::{AtomicBool, Ordering};
/// # use nix::sys::signal::{self, Signal, SigHandler};
/// lazy_static! {
/// static ref SIGNALED: AtomicBool = AtomicBool::new(false);
/// }
///
/// extern fn handle_sigint(signal: libc::c_int) {
/// let signal = Signal::from_c_int(signal).unwrap();
/// let signal = Signal::try_from(signal).unwrap();
/// SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
/// }
///
Expand Down
5 changes: 3 additions & 2 deletions src/sys/wait.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use libc::{self, c_int};
use Result;
use errno::Errno;
use std::convert::TryFrom;
use unistd::Pid;

use sys::signal::Signal;
Expand Down Expand Up @@ -126,7 +127,7 @@ fn signaled(status: i32) -> bool {
}

fn term_signal(status: i32) -> Result<Signal> {
Signal::from_c_int(unsafe { libc::WTERMSIG(status) })
Signal::try_from(unsafe { libc::WTERMSIG(status) })
}

fn dumped_core(status: i32) -> bool {
Expand All @@ -138,7 +139,7 @@ fn stopped(status: i32) -> bool {
}

fn stop_signal(status: i32) -> Result<Signal> {
Signal::from_c_int(unsafe { libc::WSTOPSIG(status) })
Signal::try_from(unsafe { libc::WSTOPSIG(status) })
}

#[cfg(any(target_os = "android", target_os = "linux"))]
Expand Down
3 changes: 2 additions & 1 deletion test/sys/test_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use libc;
use nix::Error;
use nix::sys::signal::*;
use nix::unistd::*;
use std::convert::TryFrom;
use std::sync::atomic::{AtomicBool, Ordering};

#[test]
Expand Down Expand Up @@ -75,7 +76,7 @@ lazy_static! {
}

extern fn test_sigaction_handler(signal: libc::c_int) {
let signal = Signal::from_c_int(signal).unwrap();
let signal = Signal::try_from(signal).unwrap();
SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
}

Expand Down
4 changes: 3 additions & 1 deletion test/sys/test_signalfd.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

#[test]
fn test_signalfd() {
use nix::sys::signalfd::SignalFd;
Expand All @@ -20,6 +22,6 @@ fn test_signalfd() {

// And now catch that same signal.
let res = fd.read_signal().unwrap().unwrap();
let signo = Signal::from_c_int(res.ssi_signo as i32).unwrap();
let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
assert_eq!(signo, signal::SIGUSR1);
}

0 comments on commit 33983e2

Please sign in to comment.