summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-09-04 15:49:15 +0000
committerGitHub <noreply@github.com>2019-09-04 15:49:15 +0000
commit01f4d57eede3ed2f1051778113b1d6a7dff8e9d6 (patch)
tree785be7555521b922ebea9b46c740197d255157ce
parent54cd6fe77977652256d695d565e1b1a5fe4d5613 (diff)
parent6a1c217b22017a7f37f3426259c394b1f305ff4a (diff)
downloadnix-01f4d57eede3ed2f1051778113b1d6a7dff8e9d6.zip
Merge #1113
1113: Libc enum tryfrom r=asomers a=asomers Co-authored-by: Alan Somers <asomers@gmail.com>
-rw-r--r--CHANGELOG.md3
-rw-r--r--src/macros.rs69
-rw-r--r--src/sys/signal.rs16
-rw-r--r--src/sys/wait.rs5
-rw-r--r--test/sys/test_signal.rs3
-rw-r--r--test/sys/test_signalfd.rs4
6 files changed, 29 insertions, 71 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 88b014e1..e72e5517 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
([#1107](https://github.com/nix-rust/nix/pull/1107))
### 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/macros.rs b/src/macros.rs
index 7ec00c86..5fb49e3b 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -81,9 +81,10 @@ macro_rules! libc_bitflags {
/// }
/// ```
macro_rules! libc_enum {
- // (non-pub) Exit rule.
+ // Exit rule.
(@make_enum
{
+ $v:vis
name: $BitFlags:ident,
attrs: [$($attrs:tt)*],
entries: [$($entries:tt)*],
@@ -91,49 +92,15 @@ macro_rules! libc_enum {
) => {
$($attrs)*
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
- enum $BitFlags {
+ $v enum $BitFlags {
$($entries)*
}
};
- // (pub) Exit rule.
- (@make_enum
- {
- pub,
- name: $BitFlags:ident,
- attrs: [$($attrs:tt)*],
- entries: [$($entries:tt)*],
- }
- ) => {
- $($attrs)*
- #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
- pub enum $BitFlags {
- $($entries)*
- }
- };
-
- // (non-pub) Done accumulating.
- (@accumulate_entries
- {
- name: $BitFlags:ident,
- attrs: $attrs:tt,
- },
- $entries:tt;
- ) => {
- libc_enum! {
- @make_enum
- {
- name: $BitFlags,
- attrs: $attrs,
- entries: $entries,
- }
- }
- };
-
- // (pub) Done accumulating.
+ // Done accumulating.
(@accumulate_entries
{
- pub,
+ $v:vis
name: $BitFlags:ident,
attrs: $attrs:tt,
},
@@ -142,7 +109,7 @@ macro_rules! libc_enum {
libc_enum! {
@make_enum
{
- pub,
+ $v
name: $BitFlags,
attrs: $attrs,
entries: $entries,
@@ -217,35 +184,17 @@ macro_rules! libc_enum {
}
};
- // (non-pub) Entry rule.
- (
- $(#[$attr:meta])*
- enum $BitFlags:ident {
- $($vals:tt)*
- }
- ) => {
- libc_enum! {
- @accumulate_entries
- {
- name: $BitFlags,
- attrs: [$(#[$attr])*],
- },
- [];
- $($vals)*
- }
- };
-
- // (pub) Entry rule.
+ // Entry rule.
(
$(#[$attr:meta])*
- pub enum $BitFlags:ident {
+ $v:vis enum $BitFlags:ident {
$($vals:tt)*
}
) => {
libc_enum! {
@accumulate_entries
{
- pub,
+ $v
name: $BitFlags,
attrs: [$(#[$attr])*],
},
diff --git a/src/sys/signal.rs b/src/sys/signal.rs
index 41a62e6a..1224888e 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<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 {
@@ -414,7 +415,7 @@ impl SigSet {
let res = unsafe { libc::sigwait(&self.sigset as *const libc::sigset_t, signum.as_mut_ptr()) };
Errno::result(res).map(|_| unsafe {
- Signal::from_c_int(signum.assume_init()).unwrap()
+ Signal::try_from(signum.assume_init()).unwrap()
})
}
}
@@ -542,6 +543,7 @@ 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! {
@@ -549,7 +551,7 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigActi
/// }
///
/// 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);
/// }
///
diff --git a/src/sys/wait.rs b/src/sys/wait.rs
index 7b749990..d18c3757 100644
--- a/src/sys/wait.rs
+++ b/src/sys/wait.rs
@@ -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;
@@ -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 {
@@ -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"))]
diff --git a/test/sys/test_signal.rs b/test/sys/test_signal.rs
index 8780763f..11875750 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 a3b60988..92759a48 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);
}