Created
September 9, 2017 15:55
-
-
Save brunoczim/2778f4b4cbb68bb427130a8d239da235 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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