Skip to content

Instantly share code, notes, and snippets.

@kamalmarhubi
Created March 15, 2016 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamalmarhubi/810ec26a3757acb001d2 to your computer and use it in GitHub Desktop.
Save kamalmarhubi/810ec26a3757acb001d2 to your computer and use it in GitHub Desktop.
Macro that wraps bitflags! from bitflags
/// The `libc_bitflags!` macro helps with a common use case of defining bitflags with values from
/// the libc crate. It is used the same way as the `bitflags!` macro, except that only the
/// name of the flag value has to be given.
///
/// # Example
///
/// ```
/// ```
///
///
//#[macro_export]
macro_rules! libc_bitflags {
// Exit rule.
// (@call_bitflags pub $BitFlags:ident $T:ty ($(attrs:tt)*) ($($flags:tt)*)) => {
// bitflags!(
// $($attrs)*
// pub flags $BitFlags: $T {
// $($flags)*
// }
// );
// };
// Exit rule.
(@call_bitflags $BitFlags:ident; $T:ty; ($(attrs:tt)*); ($($flags:tt)*);) => {
bitflags!(
$($attrs)*
flags $BitFlags: $T {
$($flags)*
}
);
};
// Done accumulating.
// (@accumulate_flags
// pub $BitFlags:ident $T:ty ($(attrs:tt)*) ($(flags:tt)*)) => {
// libc_bitflags!(@call_bitflags pub $BitFlags $T ($(attrs)*) ($($flags)*))
// };
// Done accumulating.
(@accumulate_flags
$BitFlags:ident; $T:ty; ($(attrs:tt)*); ($(flags:tt)*);) => {
libc_bitflags!(@call_bitflags $BitFlags; $T; ($(attrs)*); ($($flags)*);)
};
// Munch an attr.
(@accumulate_flags
$BitFlags:ident; $T:ty; ($($attrs:tt)*); ($($flags:tt)*); #[$attr:meta] $($tail:tt)*) => {
libc_bitflags!(@accumulate_flags $BitFlags; $T; ($($attrs)*);
($($flags)* #[$attr]); $($tail)*);
};
// Munch last ident if not followed by a comma.
(@accumulate_flags
$BitFlags:ident; $T:ty; ($($attrs:tt)*); ($($flags:tt)*); $flag:ident) => {
libc_bitflags!(@accumulate_flags $BitFlags; $T; ($($attrs)*);
($($flags)* const $flag = libc::$flag,));
};
// Munch an ident; covers terminating comma case.
(@accumulate_flags
$BitFlags:ident; $T:ty; ($($attrs:tt)*); ($($flags:tt)*); $flag:ident, $($tail:tt)*) => {
libc_bitflags!(@accumulate_flags $BitFlags; $T; ($($attrs)*);
($($flags)* const $flag = libc::$flag,); $($tail)*);
};
// Entry rules without `pub`.
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
$($vals:tt)*
}) => {
libc_bitflags!(@accumulate_flags
$BitFlags; $T; ($(#[$attr])*); (); $($vals)*);
};
}
//#[macro_export]
//macro_rules! libc_bitflags {
// () => {};
// (#[$attr:meta] $($tail:tt)*)=> {
// #[$attr]
// libc_bitflags!($($tail)*)
// };
// ($f
//macro_rules! libc_bitflags {
// (flags $flags:ident: $ty:ty {
// $($id:ident),* $(,)*
// }) => (
// bitflags!(
// flags $flags: $ty {
// $(const $id = libc::$id),*
// }
// );
// )
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment