Skip to content

Commit 7c02039

Browse files
committed
Allow casting a flag to a type in libc_bitflags!
This is necessary because certain flags in libc have different types, generally due to a mistake when adding the flags to the libc. See rust-lang/libc#511 for an example of such a discrepency.
1 parent e12ff77 commit 7c02039

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/macros.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@
1919
/// }
2020
/// }
2121
/// ```
22+
///
23+
/// Example with casting, due to a mistake in libc. In this example, the
24+
/// various flags have different types, so we cast the broken ones to the right
25+
/// type.
26+
///
27+
/// ```
28+
/// libc_bitflags!{
29+
/// pub flags SaFlags: libc::c_ulong {
30+
/// SA_NOCLDSTOP as libc::c_ulong,
31+
/// SA_NOCLDWAIT,
32+
/// SA_NODEFER as libc::c_ulong,
33+
/// SA_ONSTACK,
34+
/// SA_RESETHAND as libc::c_ulong,
35+
/// SA_RESTART as libc::c_ulong,
36+
/// SA_SIGINFO,
37+
/// }
38+
/// }
39+
/// ```
2240
macro_rules! libc_bitflags {
2341
// (non-pub) Exit rule.
2442
(@call_bitflags
@@ -130,6 +148,22 @@ macro_rules! libc_bitflags {
130148
}
131149
};
132150

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+
133167
// Munch an ident; covers terminating comma case.
134168
(@accumulate_flags
135169
$prefix:tt,
@@ -147,6 +181,24 @@ macro_rules! libc_bitflags {
147181
}
148182
};
149183

184+
// Munch an ident and cast it to the given type; covers terminating comma
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+
150202
// (non-pub) Entry rule.
151203
(
152204
$(#[$attr:meta])*

0 commit comments

Comments
 (0)