diff --git a/CHANGELOG.md b/CHANGELOG.md index 91af769efe..010c159162 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/src/sys/signal.rs b/src/sys/signal.rs index 1013a77fd4..52a316f2c9 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -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; @@ -288,12 +289,12 @@ impl Signal { pub fn iterator() -> SignalIterator { SignalIterator{next: 0} } +} + +impl TryFrom 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 { + fn try_from(signum: libc::c_int) -> Result { if 0 < signum && signum < NSIG { Ok(unsafe { mem::transmute(signum) }) } else { @@ -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()) } } @@ -536,6 +537,7 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result Result bool { } fn term_signal(status: i32) -> Result { - Signal::from_c_int(unsafe { libc::WTERMSIG(status) }) + Signal::try_from(unsafe { libc::WTERMSIG(status) }) } fn dumped_core(status: i32) -> bool { @@ -138,7 +139,7 @@ fn stopped(status: i32) -> bool { } fn stop_signal(status: i32) -> Result { - Signal::from_c_int(unsafe { libc::WSTOPSIG(status) }) + Signal::try_from(unsafe { libc::WSTOPSIG(status) }) } #[cfg(any(target_os = "android", target_os = "linux"))] diff --git a/test/sys/test_signal.rs b/test/sys/test_signal.rs index 8780763f77..11875750e2 100644 --- a/test/sys/test_signal.rs +++ b/test/sys/test_signal.rs @@ -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] @@ -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); } diff --git a/test/sys/test_signalfd.rs b/test/sys/test_signalfd.rs index a3b6098841..92759a486d 100644 --- a/test/sys/test_signalfd.rs +++ b/test/sys/test_signalfd.rs @@ -1,3 +1,5 @@ +use std::convert::TryFrom; + #[test] fn test_signalfd() { use nix::sys::signalfd::SignalFd; @@ -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); }