From eb04f8a4760e5b9c95d4cc7e1eb66c387c25f9bf Mon Sep 17 00:00:00 2001 From: Tim Overbeek <158390905+Bleachfuel@users.noreply.github.com> Date: Sun, 26 Jan 2025 23:25:29 +0100 Subject: [PATCH] Simplify derive_from_world (#17534) # Objective simplify existing implementation --------- Co-authored-by: Gino Valente <49806985+MrGVSV@users.noreply.github.com> --- crates/bevy_ecs/macros/Cargo.toml | 1 - crates/bevy_ecs/macros/src/lib.rs | 35 ++++++++----------------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/crates/bevy_ecs/macros/Cargo.toml b/crates/bevy_ecs/macros/Cargo.toml index 8368e2a9dc..f1ea54894a 100644 --- a/crates/bevy_ecs/macros/Cargo.toml +++ b/crates/bevy_ecs/macros/Cargo.toml @@ -14,7 +14,6 @@ bevy_macro_utils = { path = "../../bevy_macro_utils", version = "0.16.0-dev" } syn = { version = "2.0", features = ["full"] } quote = "1.0" proc-macro2 = "1.0" - [lints] workspace = true diff --git a/crates/bevy_ecs/macros/src/lib.rs b/crates/bevy_ecs/macros/src/lib.rs index f93dfabab9..e4cca41a4d 100644 --- a/crates/bevy_ecs/macros/src/lib.rs +++ b/crates/bevy_ecs/macros/src/lib.rs @@ -16,7 +16,7 @@ use proc_macro2::{Span, TokenStream as TokenStream2}; use quote::{format_ident, quote}; use syn::{ parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned, token::Comma, - ConstParam, Data, DataStruct, DeriveInput, GenericParam, Index, Token, TypeParam, + ConstParam, Data, DataStruct, DeriveInput, GenericParam, Index, TypeParam, }; enum BundleFieldKind { @@ -632,7 +632,7 @@ pub fn derive_from_world(input: TokenStream) -> TokenStream { None => { return syn::Error::new( Span::call_site(), - "No #[from_world] attribute was found on any of this enum's variants.", + "No variant found with the `#[from_world]` attribute", ) .into_compile_error() .into(); @@ -650,37 +650,20 @@ pub fn derive_from_world(input: TokenStream) -> TokenStream { }; let field_init_expr = quote!(#bevy_ecs_path::world::FromWorld::from_world(world)); - - let field_initializers: Punctuated = match fields { - syn::Fields::Named(fields_named) => fields_named - .named - .iter() - .map(|field| { - let ident = field.ident.clone().unwrap(); - quote!(#ident: #field_init_expr) - }) - .collect(), - syn::Fields::Unnamed(fields_unnamed) => (0..fields_unnamed.unnamed.len()) - .map(|index| quote!(#index: #field_init_expr)) - .collect(), - syn::Fields::Unit => Punctuated::new(), - }; - - let field_initializers: TokenStream2 = if !field_initializers.is_empty() { - quote!({ #field_initializers }) - } else { - quote!(#field_initializers) - }; + let members = fields.members(); let field_initializers = match variant_ident { - Some(variant_ident) => quote!( Self::#variant_ident #field_initializers), - None => quote!( Self #field_initializers), + Some(variant_ident) => quote!( Self::#variant_ident { + #(#members: #field_init_expr),* + }), + None => quote!( Self { + #(#members: #field_init_expr),* + }), }; TokenStream::from(quote! { impl #impl_generics #bevy_ecs_path::world::FromWorld for #name #ty_generics #where_clauses { fn from_world(world: &mut #bevy_ecs_path::world::World) -> Self { - #[allow(clippy::init_numbered_fields)] #field_initializers } }