-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Derive Macro for MapEntites (#17611) #18311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ff3b8ef
setup work space
Wuketuke 5e05092
implemented MapEntities for struct
Wuketuke 794e971
added skip mapping helper attribute
Wuketuke c220887
added documentation
Wuketuke a0029a2
corrected spelling mistakes
Wuketuke 4a65741
simplified code + improved documentation
Wuketuke eeb3c54
added support for enums
Wuketuke a65eafe
fixed small issues + added unit test
Wuketuke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Data, DeriveInput, Fields}; | ||
|
||
pub(super) fn derive_map_entities(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as DeriveInput); | ||
let name = input.ident.clone(); | ||
|
||
match input.data { | ||
Data::Struct(data_struct) => map_struct(name, &data_struct.fields), | ||
Data::Enum(data_enum) => map_enum(name, &data_enum.variants), | ||
Data::Union(_) => panic!("MapEntities is only valid on structs or enums"), | ||
} | ||
.into() | ||
} | ||
|
||
fn map_struct(name: syn::Ident, fields: &Fields) -> proc_macro2::TokenStream { | ||
let mut map_entities = vec![]; | ||
|
||
for (i, field) in fields.iter().enumerate() { | ||
if skip_mapping(&field.attrs) { | ||
continue; | ||
} | ||
|
||
let ty = &field.ty; | ||
let member = match &field.ident { | ||
// a named field (struct) | ||
Some(field_name) => syn::Member::Named(field_name.clone()), | ||
// Unnamed field (tuple-like struct) | ||
None => syn::Member::Unnamed(syn::Index::from(i)), | ||
}; | ||
|
||
let map_field = quote! { | ||
<#ty as bevy_ecs::entity::MapEntities>::map_entities(&mut self.#member, entity_mapper); | ||
}; | ||
|
||
map_entities.push(map_field); | ||
} | ||
|
||
quote! { | ||
impl MapEntities for #name { | ||
fn map_entities<M: bevy_ecs::entity::EntityMapper>(&mut self, entity_mapper: &mut M) { | ||
#(#map_entities)* | ||
} | ||
} | ||
} | ||
} | ||
|
||
// fn map_enum(name:syn::Ident, variantes: &[]) | ||
|
||
fn map_enum( | ||
name: syn::Ident, | ||
variants: &syn::punctuated::Punctuated<syn::Variant, syn::token::Comma>, | ||
) -> proc_macro2::TokenStream { | ||
let variants = variants.iter().map(|variant| { | ||
let variant_name = &variant.ident; | ||
let pattern = get_enum_pattern(&variant.fields); | ||
let map_entities = map_enum_variant(&variant.fields); | ||
|
||
quote! { | ||
#name::#variant_name #pattern => { | ||
#map_entities | ||
} | ||
} | ||
}); | ||
|
||
quote! { | ||
impl MapEntities for #name { | ||
fn map_entities<M: bevy_ecs::entity::EntityMapper>(&mut self, entity_mapper: &mut M) { | ||
match self { | ||
#(#variants)* | ||
// allows empty enums to derive aswell | ||
_ => unreachable!() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Generates the match pattern for unnamed fields (e.g., `(field_0, field_1)`) | ||
/// | ||
/// Or named fields (e.g., `(item, quantity)`) | ||
fn get_enum_pattern(fields: &Fields) -> proc_macro2::TokenStream { | ||
match fields { | ||
Fields::Named(named_fields) => { | ||
let field_vars = named_fields.named.iter().map(|field| { | ||
let var_name = field.ident.as_ref().unwrap(); | ||
quote! { #var_name } | ||
}); | ||
quote! { {#(#field_vars),*} } | ||
} | ||
Fields::Unnamed(unnamed_fields) => { | ||
let field_vars = (0..unnamed_fields.unnamed.len()).map(|i| { | ||
let var_name = | ||
syn::Ident::new(&format!("field_{}", i), proc_macro2::Span::call_site()); | ||
quote! { #var_name } | ||
}); | ||
quote! { ( #(#field_vars),* ) } | ||
} | ||
Fields::Unit => quote! {}, | ||
} | ||
} | ||
|
||
// Function to generate code to map fields for an enum variant | ||
fn map_enum_variant(fields: &Fields) -> proc_macro2::TokenStream { | ||
let mut map_entities = vec![]; | ||
|
||
for (i, field) in fields.iter().enumerate() { | ||
if skip_mapping(&field.attrs) { | ||
continue; | ||
} | ||
|
||
let ty = &field.ty; | ||
let member = match &field.ident { | ||
// named field | ||
Some(field_name) => field_name.clone(), | ||
// Unnamed field | ||
None => syn::Ident::new(&format!("field_{}", i), proc_macro2::Span::call_site()), | ||
}; | ||
|
||
let map_field = quote! { | ||
<#ty as bevy_ecs::entity::MapEntities>::map_entities(#member, entity_mapper); | ||
}; | ||
|
||
map_entities.push(map_field); | ||
} | ||
|
||
quote! { | ||
#(#map_entities)* | ||
} | ||
} | ||
|
||
/// check if any attribute contains `skip_mapping` | ||
fn skip_mapping(attrs: &[syn::Attribute]) -> bool { | ||
for attr in attrs { | ||
if !attr.path().is_ident("skip_mapping") { | ||
continue; | ||
} | ||
match &attr.meta { | ||
// skip that fields | ||
syn::Meta::Path(_) => return true, | ||
// the attribute has been used incorrectly | ||
_ => panic!("just use the bare `#[skip_mapping]`"), | ||
} | ||
} | ||
|
||
false | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An extra
#
hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to eliminate that double newline?