Created
January 13, 2020 07:47
-
-
Save internetimagery/c37593b0c8665b4a08b5a61f131b0062 to your computer and use it in GitHub Desktop.
Map a named struct to enum entries.
This file contains 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
macro_rules! collection { | |
(enum $enum_name:ident {$($variant_name:ident {$($field_name:ident: $field_type:ty$(,)*)*},)+}) => { | |
$(struct $variant_name { | |
$($field_name: $field_type,)* | |
})+ | |
enum $enum_name { | |
$($variant_name($variant_name),)+ | |
} | |
} | |
} | |
//////////////////////////////// | |
collection!{ | |
enum Action { | |
Walking {}, | |
Running {speed: i32}, | |
} | |
} | |
fn main() { | |
Action::Walking(Walking{}); | |
Action::Running(Running{speed:123}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment