Skip to content

Commit 540f1f3

Browse files
authored
Use u32 to represent most flags and enum types. (#701)
This makes the types consistent between the linux_raw and libc backends. And, using unsigned types for flags is more idiomatic for Rust. To avoid the risk of `as` silently casting away bits, introduce new `bitcast` and `bitflags_bits` macros which convert their argument to the needed integer type while only changing its signedness interpretation.
1 parent b1113a3 commit 540f1f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+540
-316
lines changed

src/backend/libc/event/epoll.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,62 +87,64 @@ use core::slice;
8787

8888
bitflags! {
8989
/// `EPOLL_*` for use with [`new`].
90+
#[repr(transparent)]
9091
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
91-
pub struct CreateFlags: c::c_int {
92+
pub struct CreateFlags: u32 {
9293
/// `EPOLL_CLOEXEC`
93-
const CLOEXEC = c::EPOLL_CLOEXEC;
94+
const CLOEXEC = bitcast!(c::EPOLL_CLOEXEC);
9495
}
9596
}
9697

9798
bitflags! {
9899
/// `EPOLL*` for use with [`add`].
100+
#[repr(transparent)]
99101
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
100102
pub struct EventFlags: u32 {
101103
/// `EPOLLIN`
102-
const IN = c::EPOLLIN as u32;
104+
const IN = bitcast!(c::EPOLLIN);
103105

104106
/// `EPOLLOUT`
105-
const OUT = c::EPOLLOUT as u32;
107+
const OUT = bitcast!(c::EPOLLOUT);
106108

107109
/// `EPOLLPRI`
108-
const PRI = c::EPOLLPRI as u32;
110+
const PRI = bitcast!(c::EPOLLPRI);
109111

110112
/// `EPOLLERR`
111-
const ERR = c::EPOLLERR as u32;
113+
const ERR = bitcast!(c::EPOLLERR);
112114

113115
/// `EPOLLHUP`
114-
const HUP = c::EPOLLHUP as u32;
116+
const HUP = bitcast!(c::EPOLLHUP);
115117

116118
/// `EPOLLRDNORM`
117-
const RDNORM = c::EPOLLRDNORM as u32;
119+
const RDNORM = bitcast!(c::EPOLLRDNORM);
118120

119121
/// `EPOLLRDBAND`
120-
const RDBAND = c::EPOLLRDBAND as u32;
122+
const RDBAND = bitcast!(c::EPOLLRDBAND);
121123

122124
/// `EPOLLWRNORM`
123-
const WRNORM = c::EPOLLWRNORM as u32;
125+
const WRNORM = bitcast!(c::EPOLLWRNORM);
124126

125127
/// `EPOLLWRBAND`
126-
const WRBAND = c::EPOLLWRBAND as u32;
128+
const WRBAND = bitcast!(c::EPOLLWRBAND);
127129

128130
/// `EPOLLMSG`
129-
const MSG = c::EPOLLMSG as u32;
131+
const MSG = bitcast!(c::EPOLLMSG);
130132

131133
/// `EPOLLRDHUP`
132-
const RDHUP = c::EPOLLRDHUP as u32;
134+
const RDHUP = bitcast!(c::EPOLLRDHUP);
133135

134136
/// `EPOLLET`
135-
const ET = c::EPOLLET as u32;
137+
const ET = bitcast!(c::EPOLLET);
136138

137139
/// `EPOLLONESHOT`
138-
const ONESHOT = c::EPOLLONESHOT as u32;
140+
const ONESHOT = bitcast!(c::EPOLLONESHOT);
139141

140142
/// `EPOLLWAKEUP`
141-
const WAKEUP = c::EPOLLWAKEUP as u32;
143+
const WAKEUP = bitcast!(c::EPOLLWAKEUP);
142144

143145
/// `EPOLLEXCLUSIVE`
144146
#[cfg(not(target_os = "android"))]
145-
const EXCLUSIVE = c::EPOLLEXCLUSIVE as u32;
147+
const EXCLUSIVE = bitcast!(c::EPOLLEXCLUSIVE);
146148
}
147149
}
148150

@@ -155,7 +157,7 @@ bitflags! {
155157
pub fn create(flags: CreateFlags) -> io::Result<OwnedFd> {
156158
// SAFETY: We're calling `epoll_create1` via FFI and we know how it
157159
// behaves.
158-
unsafe { ret_owned_fd(c::epoll_create1(flags.bits())) }
160+
unsafe { ret_owned_fd(c::epoll_create1(bitflags_bits!(flags))) }
159161
}
160162

161163
/// `epoll_ctl(self, EPOLL_CTL_ADD, data, event)`—Adds an element to an

src/backend/libc/event/poll_fd.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bitflags! {
1313
/// `POLL*` flags for use with [`poll`].
1414
///
1515
/// [`poll`]: crate::io::poll
16+
#[repr(transparent)]
1617
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
1718
pub struct PollFlags: c::c_short {
1819
/// `POLLIN`

src/backend/libc/event/syscalls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn eventfd(initval: u32, flags: EventfdFlags) -> io::Result<OwnedFd>
3131

3232
#[cfg(any(target_os = "freebsd", target_os = "illumos"))]
3333
unsafe {
34-
ret_owned_fd(c::eventfd(initval, flags.bits()))
34+
ret_owned_fd(c::eventfd(initval, bitflags_bits!(flags)))
3535
}
3636
}
3737

src/backend/libc/event/types.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ bitflags! {
66
/// `EFD_*` flags for use with [`eventfd`].
77
///
88
/// [`eventfd`]: crate::io::eventfd
9+
#[repr(transparent)]
910
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
10-
pub struct EventfdFlags: c::c_int {
11+
pub struct EventfdFlags: u32 {
1112
/// `EFD_CLOEXEC`
12-
const CLOEXEC = c::EFD_CLOEXEC;
13+
const CLOEXEC = bitcast!(c::EFD_CLOEXEC);
1314
/// `EFD_NONBLOCK`
14-
const NONBLOCK = c::EFD_NONBLOCK;
15+
const NONBLOCK = bitcast!(c::EFD_NONBLOCK);
1516
/// `EFD_SEMAPHORE`
16-
const SEMAPHORE = c::EFD_SEMAPHORE;
17+
const SEMAPHORE = bitcast!(c::EFD_SEMAPHORE);
1718
}
1819
}

src/backend/libc/fs/inotify.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ bitflags! {
1010
/// `IN_*` for use with [`inotify_init`].
1111
///
1212
/// [`inotify_init`]: crate::fs::inotify::inotify_init
13+
#[repr(transparent)]
1314
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
14-
pub struct CreateFlags: c::c_int {
15+
pub struct CreateFlags: u32 {
1516
/// `IN_CLOEXEC`
16-
const CLOEXEC = c::IN_CLOEXEC;
17+
const CLOEXEC = bitcast!(c::IN_CLOEXEC);
1718
/// `IN_NONBLOCK`
18-
const NONBLOCK = c::IN_NONBLOCK;
19+
const NONBLOCK = bitcast!(c::IN_NONBLOCK);
1920
}
2021
}
2122

2223
bitflags! {
2324
/// `IN*` for use with [`inotify_add_watch`].
2425
///
2526
/// [`inotify_add_watch`]: crate::fs::inotify::inotify_add_watch
27+
#[repr(transparent)]
2628
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Debug)]
2729
pub struct WatchFlags: u32 {
2830
/// `IN_ACCESS`
@@ -79,7 +81,7 @@ bitflags! {
7981
#[doc(alias = "inotify_init1")]
8082
pub fn inotify_init(flags: CreateFlags) -> io::Result<OwnedFd> {
8183
// SAFETY: `inotify_init1` has no safety preconditions.
82-
unsafe { ret_owned_fd(c::inotify_init1(flags.bits())) }
84+
unsafe { ret_owned_fd(c::inotify_init1(bitflags_bits!(flags))) }
8385
}
8486

8587
/// `inotify_add_watch(self, path, flags)`—Adds a watch to inotify

0 commit comments

Comments
 (0)