Skip to content

Instantly share code, notes, and snippets.

@brunoczim
Created September 9, 2017 15:55
Show Gist options
  • Save brunoczim/2778f4b4cbb68bb427130a8d239da235 to your computer and use it in GitHub Desktop.
Save brunoczim/2778f4b4cbb68bb427130a8d239da235 to your computer and use it in GitHub Desktop.
/// Aims to create enumerated constants.
/// # Example
/// ```
/// pub struct Foo;
/// impl Foo {
/// enum_consts! {
/// pub const: u32 =>
/// BAR,
/// BAZ,
/// }
/// }
///
/// pub struct Foo2;
/// impl Foo2 {
/// enum_consts! {
/// pub const: u32 = 15 =>
/// BAR,
/// BAZ,
/// SUPER_FOO,
/// }
/// }
/// ```
#[macro_export]
macro_rules! enum_consts {
{$($flag:ident)+ : $ty:ty => $($name:ident),*} => {
enum_consts! {$($flag)*: $ty = 0 => $($name),*}
};
{$($flag:ident)+ : $ty:ty => $($name:ident,)*} => {
enum_consts! {$($flag)*: $ty => $($name),*}
};
{$($flag:ident)+ : $ty:ty = $val:expr => $name_1:ident $(,$name:ident)*} => {
$($flag)* $name_1: $ty = $val;
enum_consts! {$($flag)*: $ty = ($val + 1) => $($name),*}
};
{$($flag:ident)+ : $ty:ty = $val:expr => $($name:ident,)+ } => {
enum_consts! {$($flag)*: $ty = $val => $($name),*}
};
{$($flag:ident)+ : $ty:ty = $val:expr => } => {};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment