Skip to content

Libc enum tryfrom #1113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
69 changes: 9 additions & 60 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,59 +81,26 @@ 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)*],
}
) => {
$($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,
},
Expand All @@ -142,7 +109,7 @@ macro_rules! libc_enum {
libc_enum! {
@make_enum
{
pub,
$v
name: $BitFlags,
attrs: $attrs,
entries: $entries,
Expand Down Expand Up @@ -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])*],
},
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 @@ -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()
})
}
}
Expand Down Expand Up @@ -542,14 +543,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);
}