forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EnumVariantMeta derive (bevyengine#1972)
There are cases where we want an enum variant name. Right now the only way to do that with rust's std is to derive Debug, but this will also print out the variant's fields. This creates the unfortunate situation where we need to manually write out each variant's string name (ex: in bevyengine#1963), which is both boilerplate-ey and error-prone. Crates such as `strum` exist for this reason, but it includes a lot of code and complexity that we don't need. This adds a dead-simple `EnumVariantMeta` derive that exposes `enum_variant_index` and `enum_variant_name` functions. This allows us to make cases like bevyengine#1963 much cleaner (see the second commit). We might also be able to reuse this logic for `bevy_reflect` enum derives.
- Loading branch information
1 parent
126702f
commit 0d34b0e
Showing
8 changed files
with
99 additions
and
123 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::modules::{get_modules, get_path}; | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Data, DeriveInput}; | ||
|
||
pub fn derive_enum_variant_meta(input: TokenStream) -> TokenStream { | ||
let ast = parse_macro_input!(input as DeriveInput); | ||
let variants = match &ast.data { | ||
Data::Enum(v) => &v.variants, | ||
_ => panic!("Expected an enum."), | ||
}; | ||
|
||
let modules = get_modules(&ast.attrs); | ||
let bevy_util_path = get_path(&modules.bevy_utils); | ||
|
||
let generics = ast.generics; | ||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); | ||
|
||
let struct_name = &ast.ident; | ||
let idents = variants.iter().map(|v| &v.ident); | ||
let names = variants.iter().map(|v| v.ident.to_string()); | ||
let indices = 0..names.len(); | ||
|
||
TokenStream::from(quote! { | ||
impl #impl_generics #bevy_util_path::EnumVariantMeta for #struct_name#ty_generics #where_clause { | ||
fn enum_variant_index(&self) -> usize { | ||
match self { | ||
#(#struct_name::#idents {..} => #indices,)* | ||
} | ||
} | ||
fn enum_variant_name(&self) -> &'static str { | ||
static variants: &[&str] = &[ | ||
#(#names,)* | ||
]; | ||
let index = self.enum_variant_index(); | ||
variants[index] | ||
} | ||
} | ||
}) | ||
} |
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
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
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
Oops, something went wrong.