Skip to content

Commit b70a10a

Browse files
author
Bryant Mairs
committed
Upgrade to Bitflags 1.0
The libc_bitflags! macro was replaced with a non-recursive one supporting only public structs. I could not figure out how to make the old macro work with the upgrade, so I reworked part of the bitflags! macro directly to suit our needs, much as the original recursive macro was made. There are no uses of this macro for non-public structs, so this is not a problem for internal code.
1 parent 6a2feaa commit b70a10a

File tree

7 files changed

+55
-234
lines changed

7 files changed

+55
-234
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exclude = [
1616

1717
[dependencies]
1818
libc = { git = "https://github.com/rust-lang/libc" }
19-
bitflags = "0.9"
19+
bitflags = "1.0"
2020
cfg-if = "0.1.0"
2121
void = "1.0.2"
2222

src/macros.rs

Lines changed: 16 additions & 195 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/// The `libc_bitflags!` macro helps with a common use case of defining bitflags with values from
2-
/// the libc crate. It is used the same way as the `bitflags!` macro, except that only the name of
3-
/// the flag value has to be given.
1+
/// The `libc_bitflags!` macro helps with a common use case of defining a public bitflags type
2+
/// with values from the libc crate. It is used the same way as the `bitflags!` macro, except
3+
/// that only the name of the flag value has to be given.
44
///
55
/// The `libc` crate must be in scope with the name `libc`.
66
///
@@ -10,6 +10,7 @@
1010
/// pub struct ProtFlags: libc::c_int {
1111
/// PROT_NONE;
1212
/// PROT_READ;
13+
/// /// PROT_WRITE enables write protect
1314
/// PROT_WRITE;
1415
/// PROT_EXEC;
1516
/// #[cfg(any(target_os = "linux", target_os = "android"))]
@@ -38,205 +39,25 @@
3839
/// }
3940
/// ```
4041
macro_rules! libc_bitflags {
41-
// (non-pub) Exit rule.
42-
(@call_bitflags
43-
{
44-
name: $BitFlags:ident,
45-
type: $T:ty,
46-
attrs: [$($attrs:tt)*],
47-
flags: [$($flags:tt)*],
48-
}
49-
) => {
50-
bitflags! {
51-
$($attrs)*
52-
struct $BitFlags: $T {
53-
$($flags)*
54-
}
55-
}
56-
};
57-
58-
// (pub) Exit rule.
59-
(@call_bitflags
60-
{
61-
pub,
62-
name: $BitFlags:ident,
63-
type: $T:ty,
64-
attrs: [$($attrs:tt)*],
65-
flags: [$($flags:tt)*],
42+
(
43+
$(#[$outer:meta])*
44+
pub struct $BitFlags:ident: $T:ty {
45+
$(
46+
$(#[$inner:ident $($args:tt)*])*
47+
$Flag:ident $(as $cast:ty)*;
48+
)+
6649
}
6750
) => {
6851
bitflags! {
69-
$($attrs)*
52+
$(#[$outer])*
7053
pub struct $BitFlags: $T {
71-
$($flags)*
72-
}
73-
}
74-
};
75-
76-
// (non-pub) Done accumulating.
77-
(@accumulate_flags
78-
{
79-
name: $BitFlags:ident,
80-
type: $T:ty,
81-
attrs: $attrs:tt,
82-
},
83-
$flags:tt;
84-
) => {
85-
libc_bitflags! {
86-
@call_bitflags
87-
{
88-
name: $BitFlags,
89-
type: $T,
90-
attrs: $attrs,
91-
flags: $flags,
54+
$(
55+
$(#[$inner $($args)*])*
56+
const $Flag = libc::$Flag $(as $cast)*;
57+
)+
9258
}
9359
}
9460
};
95-
96-
// (pub) Done accumulating.
97-
(@accumulate_flags
98-
{
99-
pub,
100-
name: $BitFlags:ident,
101-
type: $T:ty,
102-
attrs: $attrs:tt,
103-
},
104-
$flags:tt;
105-
) => {
106-
libc_bitflags! {
107-
@call_bitflags
108-
{
109-
pub,
110-
name: $BitFlags,
111-
type: $T,
112-
attrs: $attrs,
113-
flags: $flags,
114-
}
115-
}
116-
};
117-
118-
// Munch an attr.
119-
(@accumulate_flags
120-
$prefix:tt,
121-
[$($flags:tt)*];
122-
#[$attr:meta] $($tail:tt)*
123-
) => {
124-
libc_bitflags! {
125-
@accumulate_flags
126-
$prefix,
127-
[
128-
$($flags)*
129-
#[$attr]
130-
];
131-
$($tail)*
132-
}
133-
};
134-
135-
// Munch last ident if not followed by a semicolon.
136-
(@accumulate_flags
137-
$prefix:tt,
138-
[$($flags:tt)*];
139-
$flag:ident
140-
) => {
141-
libc_bitflags! {
142-
@accumulate_flags
143-
$prefix,
144-
[
145-
$($flags)*
146-
const $flag = libc::$flag;
147-
];
148-
}
149-
};
150-
151-
// Munch last ident and cast it to the given type.
152-
(@accumulate_flags
153-
$prefix:tt,
154-
[$($flags:tt)*];
155-
$flag:ident as $ty:ty
156-
) => {
157-
libc_bitflags! {
158-
@accumulate_flags
159-
$prefix,
160-
[
161-
$($flags)*
162-
const $flag = libc::$flag as $ty;
163-
];
164-
}
165-
};
166-
167-
// Munch an ident; covers terminating semicolon case.
168-
(@accumulate_flags
169-
$prefix:tt,
170-
[$($flags:tt)*];
171-
$flag:ident; $($tail:tt)*
172-
) => {
173-
libc_bitflags! {
174-
@accumulate_flags
175-
$prefix,
176-
[
177-
$($flags)*
178-
const $flag = libc::$flag;
179-
];
180-
$($tail)*
181-
}
182-
};
183-
184-
// Munch an ident and cast it to the given type; covers terminating semicolon
185-
// case.
186-
(@accumulate_flags
187-
$prefix:tt,
188-
[$($flags:tt)*];
189-
$flag:ident as $ty:ty; $($tail:tt)*
190-
) => {
191-
libc_bitflags! {
192-
@accumulate_flags
193-
$prefix,
194-
[
195-
$($flags)*
196-
const $flag = libc::$flag as $ty;
197-
];
198-
$($tail)*
199-
}
200-
};
201-
202-
// (non-pub) Entry rule.
203-
(
204-
$(#[$attr:meta])*
205-
struct $BitFlags:ident: $T:ty {
206-
$($vals:tt)*
207-
}
208-
) => {
209-
libc_bitflags! {
210-
@accumulate_flags
211-
{
212-
name: $BitFlags,
213-
type: $T,
214-
attrs: [$(#[$attr])*],
215-
},
216-
[];
217-
$($vals)*
218-
}
219-
};
220-
221-
// (pub) Entry rule.
222-
(
223-
$(#[$attr:meta])*
224-
pub struct $BitFlags:ident: $T:ty {
225-
$($vals:tt)*
226-
}
227-
) => {
228-
libc_bitflags! {
229-
@accumulate_flags
230-
{
231-
pub,
232-
name: $BitFlags,
233-
type: $T,
234-
attrs: [$(#[$attr])*],
235-
},
236-
[];
237-
$($vals)*
238-
}
239-
};
24061
}
24162

24263
/// The `libc_enum!` macro helps with a common use case of defining an enum exclusively using

src/mqueue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn mq_setattr(mqd: mqd_t, newattr: &MqAttr) -> Result<MqAttr> {
151151
/// Returns the old attributes
152152
pub fn mq_set_nonblock(mqd: mqd_t) -> Result<(MqAttr)> {
153153
let oldattr = try!(mq_getattr(mqd));
154-
let newattr = MqAttr::new(O_NONBLOCK.bits() as c_long,
154+
let newattr = MqAttr::new(MQ_OFlag::O_NONBLOCK.bits() as c_long,
155155
oldattr.mq_attr.mq_maxmsg,
156156
oldattr.mq_attr.mq_msgsize,
157157
oldattr.mq_attr.mq_curmsgs);

src/sys/quota.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl Dqblk {
121121
/// The absolute limit on disk quota blocks allocated.
122122
pub fn blocks_hard_limit(&self) -> Option<u64> {
123123
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
124-
if valid_fields.contains(QIF_BLIMITS) {
124+
if valid_fields.contains(QuotaValidFlags::QIF_BLIMITS) {
125125
Some(self.0.dqb_bhardlimit)
126126
} else {
127127
None
@@ -136,7 +136,7 @@ impl Dqblk {
136136
/// Preferred limit on disk quota blocks
137137
pub fn blocks_soft_limit(&self) -> Option<u64> {
138138
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
139-
if valid_fields.contains(QIF_BLIMITS) {
139+
if valid_fields.contains(QuotaValidFlags::QIF_BLIMITS) {
140140
Some(self.0.dqb_bsoftlimit)
141141
} else {
142142
None
@@ -151,7 +151,7 @@ impl Dqblk {
151151
/// Current occupied space (bytes).
152152
pub fn occupied_space(&self) -> Option<u64> {
153153
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
154-
if valid_fields.contains(QIF_SPACE) {
154+
if valid_fields.contains(QuotaValidFlags::QIF_SPACE) {
155155
Some(self.0.dqb_curspace)
156156
} else {
157157
None
@@ -161,7 +161,7 @@ impl Dqblk {
161161
/// Maximum number of allocated inodes.
162162
pub fn inodes_hard_limit(&self) -> Option<u64> {
163163
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
164-
if valid_fields.contains(QIF_ILIMITS) {
164+
if valid_fields.contains(QuotaValidFlags::QIF_ILIMITS) {
165165
Some(self.0.dqb_ihardlimit)
166166
} else {
167167
None
@@ -176,7 +176,7 @@ impl Dqblk {
176176
/// Preferred inode limit
177177
pub fn inodes_soft_limit(&self) -> Option<u64> {
178178
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
179-
if valid_fields.contains(QIF_ILIMITS) {
179+
if valid_fields.contains(QuotaValidFlags::QIF_ILIMITS) {
180180
Some(self.0.dqb_isoftlimit)
181181
} else {
182182
None
@@ -191,7 +191,7 @@ impl Dqblk {
191191
/// Current number of allocated inodes.
192192
pub fn allocated_inodes(&self) -> Option<u64> {
193193
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
194-
if valid_fields.contains(QIF_INODES) {
194+
if valid_fields.contains(QuotaValidFlags::QIF_INODES) {
195195
Some(self.0.dqb_curinodes)
196196
} else {
197197
None
@@ -201,7 +201,7 @@ impl Dqblk {
201201
/// Time limit for excessive disk use.
202202
pub fn block_time_limit(&self) -> Option<u64> {
203203
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
204-
if valid_fields.contains(QIF_BTIME) {
204+
if valid_fields.contains(QuotaValidFlags::QIF_BTIME) {
205205
Some(self.0.dqb_btime)
206206
} else {
207207
None
@@ -216,7 +216,7 @@ impl Dqblk {
216216
/// Time limit for excessive files.
217217
pub fn inode_time_limit(&self) -> Option<u64> {
218218
let valid_fields = QuotaValidFlags::from_bits_truncate(self.0.dqb_valid);
219-
if valid_fields.contains(QIF_ITIME) {
219+
if valid_fields.contains(QuotaValidFlags::QIF_ITIME) {
220220
Some(self.0.dqb_itime)
221221
} else {
222222
None

src/sys/signal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ impl SigAction {
370370
SigHandler::SigAction(f) => f as *const extern fn(libc::c_int, *mut libc::siginfo_t, *mut libc::c_void) as usize,
371371
};
372372
s.sa_flags = match handler {
373-
SigHandler::SigAction(_) => (flags | SA_SIGINFO).bits(),
374-
_ => (flags - SA_SIGINFO).bits(),
373+
SigHandler::SigAction(_) => (flags | SaFlags::SA_SIGINFO).bits(),
374+
_ => (flags - SaFlags::SA_SIGINFO).bits(),
375375
};
376376
s.sa_mask = mask.sigset;
377377

@@ -390,7 +390,7 @@ impl SigAction {
390390
match self.sigaction.sa_sigaction {
391391
libc::SIG_DFL => SigHandler::SigDfl,
392392
libc::SIG_IGN => SigHandler::SigIgn,
393-
f if self.flags().contains(SA_SIGINFO) =>
393+
f if self.flags().contains(SaFlags::SA_SIGINFO) =>
394394
SigHandler::SigAction( unsafe { mem::transmute(f) } ),
395395
f => SigHandler::Handler( unsafe { mem::transmute(f) } ),
396396
}

0 commit comments

Comments
 (0)