Skip to content

Instantly share code, notes, and snippets.

@internetimagery
Created January 13, 2020 07:47
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 internetimagery/c37593b0c8665b4a08b5a61f131b0062 to your computer and use it in GitHub Desktop.
Save internetimagery/c37593b0c8665b4a08b5a61f131b0062 to your computer and use it in GitHub Desktop.
Map a named struct to enum entries.
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