Skip to content

Commit e880a04

Browse files
committed
Use libc_enum! where possible
Some enums which use different names for values than libc still set the discriminators manually. closes #254
1 parent 1b9d205 commit e880a04

File tree

4 files changed

+135
-127
lines changed

4 files changed

+135
-127
lines changed

src/sys/aio.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,41 @@ 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 = "openbsd", target_os = "bitrig",
23+
target_os = "netbsd", target_os = "macos", target_os = "ios",
24+
target_os = "linux"))]
25+
O_DSYNC
26+
}
2627
}
2728

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
29+
libc_enum! {
30+
/// When used with `lio_listio`, determines whether a given `aiocb` should be
31+
/// used for a read operation, a write operation, or ignored. Has no effect for
32+
/// any other aio functions.
33+
#[repr(i32)]
34+
pub enum LioOpcode {
35+
LIO_NOP,
36+
LIO_WRITE,
37+
LIO_READ,
38+
}
3739
}
3840

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,
41+
libc_enum! {
42+
/// Mode for `lio_listio`.
43+
#[repr(i32)]
44+
pub enum LioMode {
45+
/// Requests that `lio_listio` block until all requested operations have
46+
/// been completed
47+
LIO_WAIT,
48+
/// Requests that `lio_listio` return immediately
49+
LIO_NOWAIT,
50+
}
4851
}
4952

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

src/sys/event.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,48 +33,50 @@ type type_of_data = libc::int64_t;
3333
#[cfg(not(target_os = "netbsd"))]
3434
type type_of_event_filter = i16;
3535
#[cfg(not(target_os = "netbsd"))]
36-
#[repr(i16)]
37-
#[derive(Clone, Copy, Debug, PartialEq)]
38-
pub enum EventFilter {
39-
EVFILT_AIO = libc::EVFILT_AIO,
40-
#[cfg(target_os = "dragonfly")]
41-
EVFILT_EXCEPT = libc::EVFILT_EXCEPT,
42-
#[cfg(any(target_os = "macos", target_os = "ios",
43-
target_os = "dragonfly",
44-
target_os = "freebsd"))]
45-
EVFILT_FS = libc::EVFILT_FS,
46-
#[cfg(target_os = "freebsd")]
47-
EVFILT_LIO = libc::EVFILT_LIO,
48-
#[cfg(any(target_os = "macos", target_os = "ios"))]
49-
EVFILT_MACHPORT = libc::EVFILT_MACHPORT,
50-
EVFILT_PROC = libc::EVFILT_PROC,
51-
EVFILT_READ = libc::EVFILT_READ,
52-
EVFILT_SIGNAL = libc::EVFILT_SIGNAL,
53-
EVFILT_TIMER = libc::EVFILT_TIMER,
54-
#[cfg(any(target_os = "macos",
55-
target_os = "ios",
56-
target_os = "dragonfly",
57-
target_os = "freebsd"))]
58-
EVFILT_USER = libc::EVFILT_USER,
59-
#[cfg(any(target_os = "macos", target_os = "ios"))]
60-
EVFILT_VM = libc::EVFILT_VM,
61-
EVFILT_VNODE = libc::EVFILT_VNODE,
62-
EVFILT_WRITE = libc::EVFILT_WRITE,
36+
libc_enum! {
37+
#[repr(i16)]
38+
pub enum EventFilter {
39+
EVFILT_AIO,
40+
#[cfg(target_os = "dragonfly")]
41+
EVFILT_EXCEPT,
42+
#[cfg(any(target_os = "macos", target_os = "ios",
43+
target_os = "dragonfly",
44+
target_os = "freebsd"))]
45+
EVFILT_FS,
46+
#[cfg(target_os = "freebsd")]
47+
EVFILT_LIO,
48+
#[cfg(any(target_os = "macos", target_os = "ios"))]
49+
EVFILT_MACHPORT,
50+
EVFILT_PROC,
51+
EVFILT_READ,
52+
EVFILT_SIGNAL,
53+
EVFILT_TIMER,
54+
#[cfg(any(target_os = "macos",
55+
target_os = "ios",
56+
target_os = "dragonfly",
57+
target_os = "freebsd"))]
58+
EVFILT_USER,
59+
#[cfg(any(target_os = "macos", target_os = "ios"))]
60+
EVFILT_VM,
61+
EVFILT_VNODE,
62+
EVFILT_WRITE,
63+
}
6364
}
6465

6566
#[cfg(target_os = "netbsd")]
6667
type type_of_event_filter = libc::uint32_t;
6768
#[cfg(target_os = "netbsd")]
69+
libc_enum! {
6870
#[repr(i32)]
69-
#[derive(Clone, Copy, Debug, PartialEq)]
70-
pub enum EventFilter {
71-
EVFILT_READ = libc::EVFILT_READ,
72-
EVFILT_WRITE = libc::EVFILT_WRITE,
73-
EVFILT_AIO = libc::EVFILT_AIO,
74-
EVFILT_VNODE = libc::EVFILT_VNODE,
75-
EVFILT_PROC = libc::EVFILT_PROC,
76-
EVFILT_SIGNAL = libc::EVFILT_SIGNAL,
77-
EVFILT_TIMER = libc::EVFILT_TIMER,
71+
pub enum EventFilter {
72+
EVFILT_READ,
73+
EVFILT_WRITE,
74+
EVFILT_AIO,
75+
EVFILT_VNODE,
76+
EVFILT_PROC,
77+
EVFILT_SIGNAL,
78+
EVFILT_TIMER,
79+
}
7880
}
7981

8082
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",

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: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,51 @@ 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 = "linux", target_os = "android", target_os = "emscripten"), not(any(target_arch = "mips", target_arch = "mips64"))))]
37+
SIGSTKFLT,
38+
SIGCHLD,
39+
SIGCONT,
40+
SIGSTOP,
41+
SIGTSTP,
42+
SIGTTIN,
43+
SIGTTOU,
44+
SIGURG,
45+
SIGXCPU,
46+
SIGXFSZ,
47+
SIGVTALRM,
48+
SIGPROF,
49+
SIGWINCH,
50+
SIGIO,
51+
#[cfg(any(target_os = "linux", target_os = "android", target_os = "emscripten"))]
52+
SIGPWR,
53+
SIGSYS,
54+
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
55+
SIGEMT,
56+
#[cfg(not(any(target_os = "linux", target_os = "android", target_os = "emscripten")))]
57+
SIGINFO,
58+
}
5859
}
5960

6061
pub use self::Signal::*;
@@ -241,12 +242,13 @@ libc_bitflags!{
241242
}
242243
}
243244

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,
245+
libc_enum! {
246+
#[repr(i32)]
247+
pub enum SigmaskHow {
248+
SIG_BLOCK,
249+
SIG_UNBLOCK,
250+
SIG_SETMASK,
251+
}
250252
}
251253

252254
#[derive(Clone, Copy)]

0 commit comments

Comments
 (0)