Skip to content

Commit 01f4d57

Browse files
bors[bot]asomers
andauthored
Merge #1113
1113: Libc enum tryfrom r=asomers a=asomers Co-authored-by: Alan Somers <[email protected]>
2 parents 54cd6fe + 6a1c217 commit 01f4d57

File tree

6 files changed

+29
-71
lines changed

6 files changed

+29
-71
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
1313
([#1107](https://github.com/nix-rust/nix/pull/1107))
1414

1515
### Changed
16+
- `Signal::from_c_int` has been replaced by `Signal::try_from`
17+
([#1113](https://github.com/nix-rust/nix/pull/1113))
18+
1619
- Changed `readlink` and `readlinkat` to return `OsString`
1720
([#1109](https://github.com/nix-rust/nix/pull/1109))
1821

src/macros.rs

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -81,59 +81,26 @@ macro_rules! libc_bitflags {
8181
/// }
8282
/// ```
8383
macro_rules! libc_enum {
84-
// (non-pub) Exit rule.
84+
// Exit rule.
8585
(@make_enum
8686
{
87+
$v:vis
8788
name: $BitFlags:ident,
8889
attrs: [$($attrs:tt)*],
8990
entries: [$($entries:tt)*],
9091
}
9192
) => {
9293
$($attrs)*
9394
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
94-
enum $BitFlags {
95+
$v enum $BitFlags {
9596
$($entries)*
9697
}
9798
};
9899

99-
// (pub) Exit rule.
100-
(@make_enum
101-
{
102-
pub,
103-
name: $BitFlags:ident,
104-
attrs: [$($attrs:tt)*],
105-
entries: [$($entries:tt)*],
106-
}
107-
) => {
108-
$($attrs)*
109-
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
110-
pub enum $BitFlags {
111-
$($entries)*
112-
}
113-
};
114-
115-
// (non-pub) Done accumulating.
116-
(@accumulate_entries
117-
{
118-
name: $BitFlags:ident,
119-
attrs: $attrs:tt,
120-
},
121-
$entries:tt;
122-
) => {
123-
libc_enum! {
124-
@make_enum
125-
{
126-
name: $BitFlags,
127-
attrs: $attrs,
128-
entries: $entries,
129-
}
130-
}
131-
};
132-
133-
// (pub) Done accumulating.
100+
// Done accumulating.
134101
(@accumulate_entries
135102
{
136-
pub,
103+
$v:vis
137104
name: $BitFlags:ident,
138105
attrs: $attrs:tt,
139106
},
@@ -142,7 +109,7 @@ macro_rules! libc_enum {
142109
libc_enum! {
143110
@make_enum
144111
{
145-
pub,
112+
$v
146113
name: $BitFlags,
147114
attrs: $attrs,
148115
entries: $entries,
@@ -217,35 +184,17 @@ macro_rules! libc_enum {
217184
}
218185
};
219186

220-
// (non-pub) Entry rule.
221-
(
222-
$(#[$attr:meta])*
223-
enum $BitFlags:ident {
224-
$($vals:tt)*
225-
}
226-
) => {
227-
libc_enum! {
228-
@accumulate_entries
229-
{
230-
name: $BitFlags,
231-
attrs: [$(#[$attr])*],
232-
},
233-
[];
234-
$($vals)*
235-
}
236-
};
237-
238-
// (pub) Entry rule.
187+
// Entry rule.
239188
(
240189
$(#[$attr:meta])*
241-
pub enum $BitFlags:ident {
190+
$v:vis enum $BitFlags:ident {
242191
$($vals:tt)*
243192
}
244193
) => {
245194
libc_enum! {
246195
@accumulate_entries
247196
{
248-
pub,
197+
$v
249198
name: $BitFlags,
250199
attrs: [$(#[$attr])*],
251200
},

src/sys/signal.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use libc;
77
use {Error, Result};
88
use errno::Errno;
9+
use std::convert::TryFrom;
910
use std::mem;
1011
use std::fmt;
1112
use std::str::FromStr;
@@ -288,12 +289,12 @@ impl Signal {
288289
pub fn iterator() -> SignalIterator {
289290
SignalIterator{next: 0}
290291
}
292+
}
293+
294+
impl TryFrom<libc::c_int> for Signal {
295+
type Error = Error;
291296

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

416417
Errno::result(res).map(|_| unsafe {
417-
Signal::from_c_int(signum.assume_init()).unwrap()
418+
Signal::try_from(signum.assume_init()).unwrap()
418419
})
419420
}
420421
}
@@ -542,14 +543,15 @@ pub unsafe fn sigaction(signal: Signal, sigaction: &SigAction) -> Result<SigActi
542543
/// # #[macro_use] extern crate lazy_static;
543544
/// # extern crate libc;
544545
/// # extern crate nix;
546+
/// # use std::convert::TryFrom;
545547
/// # use std::sync::atomic::{AtomicBool, Ordering};
546548
/// # use nix::sys::signal::{self, Signal, SigHandler};
547549
/// lazy_static! {
548550
/// static ref SIGNALED: AtomicBool = AtomicBool::new(false);
549551
/// }
550552
///
551553
/// extern fn handle_sigint(signal: libc::c_int) {
552-
/// let signal = Signal::from_c_int(signal).unwrap();
554+
/// let signal = Signal::try_from(signal).unwrap();
553555
/// SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
554556
/// }
555557
///

src/sys/wait.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use libc::{self, c_int};
22
use Result;
33
use errno::Errno;
4+
use std::convert::TryFrom;
45
use unistd::Pid;
56

67
use sys::signal::Signal;
@@ -126,7 +127,7 @@ fn signaled(status: i32) -> bool {
126127
}
127128

128129
fn term_signal(status: i32) -> Result<Signal> {
129-
Signal::from_c_int(unsafe { libc::WTERMSIG(status) })
130+
Signal::try_from(unsafe { libc::WTERMSIG(status) })
130131
}
131132

132133
fn dumped_core(status: i32) -> bool {
@@ -138,7 +139,7 @@ fn stopped(status: i32) -> bool {
138139
}
139140

140141
fn stop_signal(status: i32) -> Result<Signal> {
141-
Signal::from_c_int(unsafe { libc::WSTOPSIG(status) })
142+
Signal::try_from(unsafe { libc::WSTOPSIG(status) })
142143
}
143144

144145
#[cfg(any(target_os = "android", target_os = "linux"))]

test/sys/test_signal.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use libc;
22
use nix::Error;
33
use nix::sys::signal::*;
44
use nix::unistd::*;
5+
use std::convert::TryFrom;
56
use std::sync::atomic::{AtomicBool, Ordering};
67

78
#[test]
@@ -75,7 +76,7 @@ lazy_static! {
7576
}
7677

7778
extern fn test_sigaction_handler(signal: libc::c_int) {
78-
let signal = Signal::from_c_int(signal).unwrap();
79+
let signal = Signal::try_from(signal).unwrap();
7980
SIGNALED.store(signal == Signal::SIGINT, Ordering::Relaxed);
8081
}
8182

test/sys/test_signalfd.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryFrom;
2+
13
#[test]
24
fn test_signalfd() {
35
use nix::sys::signalfd::SignalFd;
@@ -20,6 +22,6 @@ fn test_signalfd() {
2022

2123
// And now catch that same signal.
2224
let res = fd.read_signal().unwrap().unwrap();
23-
let signo = Signal::from_c_int(res.ssi_signo as i32).unwrap();
25+
let signo = Signal::try_from(res.ssi_signo as i32).unwrap();
2426
assert_eq!(signo, signal::SIGUSR1);
2527
}

0 commit comments

Comments
 (0)