diff options
author | roblabla <unfiltered@roblab.la> | 2017-07-04 14:00:31 +0200 |
---|---|---|
committer | roblabla <unfiltered@roblab.la> | 2017-07-04 19:43:42 +0200 |
commit | 26a0a4895c86b5c869d0cab2eaad80be6c60c377 (patch) | |
tree | 7e34d28c4927677aef797dd6dd7467227e8b764d /src | |
parent | 9e51647095af4612870c0e4582b739c8147784dc (diff) | |
download | nix-26a0a4895c86b5c869d0cab2eaad80be6c60c377.zip |
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
https://github.com/rust-lang/libc/pull/511 for an example of such a
discrepency.
Diffstat (limited to 'src')
-rw-r--r-- | src/macros.rs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/macros.rs b/src/macros.rs index 046077f9..b8707d0b 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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 @@ -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, @@ -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])* |