Skip to content

Commit 31d242e

Browse files
committed
Merge #792
792: Use libc_enum! where possible r=Susurrus a=wginolas Some enums which use different names for values than libc still set the discriminators manually. closes #254
2 parents d54d874 + 7e28016 commit 31d242e

File tree

4 files changed

+102
-92
lines changed

4 files changed

+102
-92
lines changed

src/sys/aio.rs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,44 @@ use sys::time::TimeSpec;
1313

1414
/// Mode for `AioCb::fsync`. Controls whether only data or both data and
1515
/// metadata are synced.
16-
#[repr(i32)]
17-
#[derive(Clone, Copy, Debug, PartialEq)]
18-
pub enum AioFsyncMode {
19-
/// do it like `fsync`
20-
O_SYNC = libc::O_SYNC,
21-
/// on supported operating systems only, do it like `fdatasync`
22-
#[cfg(any(target_os = "openbsd", target_os = "bitrig",
23-
target_os = "netbsd", target_os = "macos", target_os = "ios",
24-
target_os = "linux"))]
25-
O_DSYNC = libc::O_DSYNC
16+
libc_enum! {
17+
#[repr(i32)]
18+
pub enum AioFsyncMode {
19+
/// do it like `fsync`
20+
O_SYNC,
21+
/// on supported operating systems only, do it like `fdatasync`
22+
#[cfg(any(target_os = "bitrig",
23+
target_os = "ios",
24+
target_os = "linux",
25+
target_os = "macos",
26+
target_os = "netbsd",
27+
target_os = "openbsd"))]
28+
O_DSYNC
29+
}
2630
}
2731

28-
/// When used with `lio_listio`, determines whether a given `aiocb` should be
29-
/// used for a read operation, a write operation, or ignored. Has no effect for
30-
/// any other aio functions.
31-
#[repr(i32)]
32-
#[derive(Clone, Copy, Debug, PartialEq)]
33-
pub enum LioOpcode {
34-
LIO_NOP = libc::LIO_NOP,
35-
LIO_WRITE = libc::LIO_WRITE,
36-
LIO_READ = libc::LIO_READ
32+
libc_enum! {
33+
/// When used with `lio_listio`, determines whether a given `aiocb` should be
34+
/// used for a read operation, a write operation, or ignored. Has no effect for
35+
/// any other aio functions.
36+
#[repr(i32)]
37+
pub enum LioOpcode {
38+
LIO_NOP,
39+
LIO_WRITE,
40+
LIO_READ,
41+
}
3742
}
3843

39-
/// Mode for `lio_listio`.
40-
#[repr(i32)]
41-
#[derive(Clone, Copy, Debug, PartialEq)]
42-
pub enum LioMode {
43-
/// Requests that `lio_listio` block until all requested operations have
44-
/// been completed
45-
LIO_WAIT = libc::LIO_WAIT,
46-
/// Requests that `lio_listio` return immediately
47-
LIO_NOWAIT = libc::LIO_NOWAIT,
44+
libc_enum! {
45+
/// Mode for `lio_listio`.
46+
#[repr(i32)]
47+
pub enum LioMode {
48+
/// Requests that `lio_listio` block until all requested operations have
49+
/// been completed
50+
LIO_WAIT,
51+
/// Requests that `lio_listio` return immediately
52+
LIO_NOWAIT,
53+
}
4854
}
4955

5056
/// Return values for `AioCb::cancel and aio_cancel_all`

src/sys/event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type type_of_data = libc::int64_t;
3434
type type_of_event_filter = u32;
3535
#[cfg(not(target_os = "netbsd"))]
3636
type type_of_event_filter = i16;
37-
libc_enum!{
37+
libc_enum! {
3838
#[cfg_attr(target_os = "netbsd", repr(u32))]
3939
#[cfg_attr(not(target_os = "netbsd"), repr(i16))]
4040
pub enum EventFilter {

src/sys/reboot.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ use libc;
55
use void::Void;
66
use std::mem::drop;
77

8-
/// How exactly should the system be rebooted.
9-
///
10-
/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
11-
/// enabling/disabling Ctrl-Alt-Delete.
12-
#[repr(i32)]
13-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
14-
pub enum RebootMode {
15-
RB_HALT_SYSTEM = libc::RB_HALT_SYSTEM,
16-
RB_KEXEC = libc::RB_KEXEC,
17-
RB_POWER_OFF = libc::RB_POWER_OFF,
18-
RB_AUTOBOOT = libc::RB_AUTOBOOT,
19-
// we do not support Restart2,
20-
RB_SW_SUSPEND = libc::RB_SW_SUSPEND,
8+
libc_enum! {
9+
/// How exactly should the system be rebooted.
10+
///
11+
/// See [`set_cad_enabled()`](fn.set_cad_enabled.html) for
12+
/// enabling/disabling Ctrl-Alt-Delete.
13+
#[repr(i32)]
14+
pub enum RebootMode {
15+
RB_HALT_SYSTEM,
16+
RB_KEXEC,
17+
RB_POWER_OFF,
18+
RB_AUTOBOOT,
19+
// we do not support Restart2,
20+
RB_SW_SUSPEND,
21+
}
2122
}
2223

2324
pub fn reboot(how: RebootMode) -> Result<Void> {

src/sys/signal.rs

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,52 @@ use std::ptr;
1111
#[cfg(not(target_os = "openbsd"))]
1212
pub use self::sigevent::*;
1313

14-
// Currently there is only one definition of c_int in libc, as well as only one
15-
// type for signal constants.
16-
// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
17-
// this is not (yet) possible.
18-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19-
#[repr(i32)]
20-
pub enum Signal {
21-
SIGHUP = libc::SIGHUP,
22-
SIGINT = libc::SIGINT,
23-
SIGQUIT = libc::SIGQUIT,
24-
SIGILL = libc::SIGILL,
25-
SIGTRAP = libc::SIGTRAP,
26-
SIGABRT = libc::SIGABRT,
27-
SIGBUS = libc::SIGBUS,
28-
SIGFPE = libc::SIGFPE,
29-
SIGKILL = libc::SIGKILL,
30-
SIGUSR1 = libc::SIGUSR1,
31-
SIGSEGV = libc::SIGSEGV,
32-
SIGUSR2 = libc::SIGUSR2,
33-
SIGPIPE = libc::SIGPIPE,
34-
SIGALRM = libc::SIGALRM,
35-
SIGTERM = libc::SIGTERM,
36-
#[cfg(all(any(target_os = "linux", target_os = "android", target_os = "emscripten"), not(any(target_arch = "mips", target_arch = "mips64"))))]
37-
SIGSTKFLT = libc::SIGSTKFLT,
38-
SIGCHLD = libc::SIGCHLD,
39-
SIGCONT = libc::SIGCONT,
40-
SIGSTOP = libc::SIGSTOP,
41-
SIGTSTP = libc::SIGTSTP,
42-
SIGTTIN = libc::SIGTTIN,
43-
SIGTTOU = libc::SIGTTOU,
44-
SIGURG = libc::SIGURG,
45-
SIGXCPU = libc::SIGXCPU,
46-
SIGXFSZ = libc::SIGXFSZ,
47-
SIGVTALRM = libc::SIGVTALRM,
48-
SIGPROF = libc::SIGPROF,
49-
SIGWINCH = libc::SIGWINCH,
50-
SIGIO = libc::SIGIO,
51-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
52-
SIGPWR = libc::SIGPWR,
53-
SIGSYS = libc::SIGSYS,
54-
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
55-
SIGEMT = libc::SIGEMT,
56-
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
57-
SIGINFO = libc::SIGINFO,
14+
libc_enum!{
15+
// Currently there is only one definition of c_int in libc, as well as only one
16+
// type for signal constants.
17+
// We would prefer to use the libc::c_int alias in the repr attribute. Unfortunately
18+
// this is not (yet) possible.
19+
#[repr(i32)]
20+
pub enum Signal {
21+
SIGHUP,
22+
SIGINT,
23+
SIGQUIT,
24+
SIGILL,
25+
SIGTRAP,
26+
SIGABRT,
27+
SIGBUS,
28+
SIGFPE,
29+
SIGKILL,
30+
SIGUSR1,
31+
SIGSEGV,
32+
SIGUSR2,
33+
SIGPIPE,
34+
SIGALRM,
35+
SIGTERM,
36+
#[cfg(all(any(target_os = "android", target_os = "emscripten", target_os = "linux"),
37+
not(any(target_arch = "mips", target_arch = "mips64"))))]
38+
SIGSTKFLT,
39+
SIGCHLD,
40+
SIGCONT,
41+
SIGSTOP,
42+
SIGTSTP,
43+
SIGTTIN,
44+
SIGTTOU,
45+
SIGURG,
46+
SIGXCPU,
47+
SIGXFSZ,
48+
SIGVTALRM,
49+
SIGPROF,
50+
SIGWINCH,
51+
SIGIO,
52+
#[cfg(any(target_os = "android", target_os = "emscripten", target_os = "linux"))]
53+
SIGPWR,
54+
SIGSYS,
55+
#[cfg(not(any(target_os = "android", target_os = "emscripten", target_os = "linux")))]
56+
SIGEMT,
57+
#[cfg(not(any(target_os = "android", target_os = "emscripten", target_os = "linux")))]
58+
SIGINFO,
59+
}
5860
}
5961

6062
pub use self::Signal::*;
@@ -241,12 +243,13 @@ libc_bitflags!{
241243
}
242244
}
243245

244-
#[repr(i32)]
245-
#[derive(Clone, Copy, PartialEq)]
246-
pub enum SigmaskHow {
247-
SIG_BLOCK = libc::SIG_BLOCK,
248-
SIG_UNBLOCK = libc::SIG_UNBLOCK,
249-
SIG_SETMASK = libc::SIG_SETMASK,
246+
libc_enum! {
247+
#[repr(i32)]
248+
pub enum SigmaskHow {
249+
SIG_BLOCK,
250+
SIG_UNBLOCK,
251+
SIG_SETMASK,
252+
}
250253
}
251254

252255
#[derive(Clone, Copy)]

0 commit comments

Comments
 (0)