Skip to content

Commit

Permalink
Allow casting a flag to a type in libc_bitflags!
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
roblabla authored and Susurrus committed Jul 4, 2017
1 parent 9e51647 commit b422927
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@
/// }
/// }
/// ```
///
/// Example with casting, due to a mistake in libc. In this example, the
/// various flags have different types, so we cast the broken ones to the right
/// type.
///
/// ```
/// libc_bitflags!{
/// pub flags SaFlags: libc::c_ulong {
/// SA_NOCLDSTOP as libc::c_ulong,
/// SA_NOCLDWAIT,
/// SA_NODEFER as libc::c_ulong,
/// SA_ONSTACK,
/// SA_RESETHAND as libc::c_ulong,
/// SA_RESTART as libc::c_ulong,
/// SA_SIGINFO,
/// }
/// }
/// ```
macro_rules! libc_bitflags {
// (non-pub) Exit rule.
(@call_bitflags
Expand Down Expand Up @@ -130,6 +148,22 @@ macro_rules! libc_bitflags {
}
};

// Munch last ident and cast it to the given type.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident as $ty:ty
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag as $ty;
];
}
};

// Munch an ident; covers terminating comma case.
(@accumulate_flags
$prefix:tt,
Expand All @@ -147,6 +181,24 @@ macro_rules! libc_bitflags {
}
};

// Munch an ident and cast it to the given type; covers terminating comma
// case.
(@accumulate_flags
$prefix:tt,
[$($flags:tt)*];
$flag:ident as $ty:ty, $($tail:tt)*
) => {
libc_bitflags! {
@accumulate_flags
$prefix,
[
$($flags)*
const $flag = libc::$flag as $ty;
];
$($tail)*
}
};

// (non-pub) Entry rule.
(
$(#[$attr:meta])*
Expand Down

0 comments on commit b422927

Please sign in to comment.