From 06392c6a2656b180fbd801be5ac92bcb4f2c6949 Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Fri, 30 Aug 2019 11:20:10 -0600 Subject: Simplify implementation of libc_enum! with vis metavariables --- src/macros.rs | 69 ++++++++--------------------------------------------------- 1 file changed, 9 insertions(+), 60 deletions(-) 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])*], }, -- cgit v1.2.3 From 6a1c217b22017a7f37f3426259c394b1f305ff4a Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Fri, 30 Aug 2019 16:33:18 -0600 Subject: Replace Signal::from_c_int by Signal::try_from TryFrom wasn't stable when that function was written. --- CHANGELOG.md | 3 +++ src/sys/signal.rs | 16 +++++++++------- src/sys/wait.rs | 5 +++-- test/sys/test_signal.rs | 3 ++- test/sys/test_signalfd.rs | 4 +++- 5 files changed, 20 insertions(+), 11 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/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 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 { @@ -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 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 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); } -- cgit v1.2.3