From c3a386f1c45da02089f1f9d533df0f6be1681c8e Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 28 Jul 2021 10:26:20 +0200 Subject: [PATCH] Fix review comments --- crates/bevy_ecs/macros/src/lib.rs | 10 +++++----- crates/bevy_ecs/src/bundle.rs | 14 ++++++-------- crates/bevy_ecs/src/lib.rs | 4 ++-- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/crates/bevy_ecs/macros/src/lib.rs b/crates/bevy_ecs/macros/src/lib.rs index eeeff34c652426..d4d307f8a53e65 100644 --- a/crates/bevy_ecs/macros/src/lib.rs +++ b/crates/bevy_ecs/macros/src/lib.rs @@ -118,7 +118,7 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream { { if *is_bundle { field_component_ids.push(quote! { - component_id.extend(<#field_type as #ecs_path::bundle::Bundle>::component_id(components)); + component_ids.extend(<#field_type as #ecs_path::bundle::Bundle>::component_ids(components)); }); field_get_components.push(quote! { self.#field.get_components(&mut func); @@ -128,7 +128,7 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream { }); } else { field_component_ids.push(quote! { - component_id.push(components.get_or_insert_id::<#field_type>()); + component_ids.push(components.get_or_insert_id::<#field_type>()); }); field_get_components.push(quote! { func((&mut self.#field as *mut #field_type).cast::()); @@ -147,12 +147,12 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream { TokenStream::from(quote! { /// SAFE: TypeInfo is returned in field-definition-order. [from_components] and [get_components] use field-definition-order unsafe impl #impl_generics #ecs_path::bundle::Bundle for #struct_name#ty_generics #where_clause { - fn component_id( + fn component_ids( components: &mut #ecs_path::component::Components, ) -> Vec<#ecs_path::component::ComponentId> { - let mut component_id = Vec::with_capacity(#field_len); + let mut component_ids = Vec::with_capacity(#field_len); #(#field_component_ids)* - component_id + component_ids } #[allow(unused_variables, unused_mut, non_snake_case)] diff --git a/crates/bevy_ecs/src/bundle.rs b/crates/bevy_ecs/src/bundle.rs index dd284773717b94..ab8f346a551164 100644 --- a/crates/bevy_ecs/src/bundle.rs +++ b/crates/bevy_ecs/src/bundle.rs @@ -44,7 +44,7 @@ use std::{any::TypeId, collections::HashMap}; /// [Bundle::component_id] pub unsafe trait Bundle: Send + Sync + 'static { /// Gets this [Bundle]'s component ids, in the order of this bundle's Components - fn component_id(components: &mut Components) -> Vec; + fn component_ids(components: &mut Components) -> Vec; /// Calls `func`, which should return data for each component in the bundle, in the order of /// this bundle's Components @@ -67,7 +67,7 @@ macro_rules! tuple_impl { /// SAFE: TypeInfo is returned in tuple-order. [Bundle::from_components] and [Bundle::get_components] use tuple-order unsafe impl<$($name: Component),*> Bundle for ($($name,)*) { #[allow(unused_variables)] - fn component_id(components: &mut Components) -> Vec { + fn component_ids(components: &mut Components) -> Vec { vec![$(components.get_or_insert_id::<$name>()),*] } @@ -206,11 +206,11 @@ impl Bundles { ) -> &'a BundleInfo { let bundle_infos = &mut self.bundle_infos; let id = self.bundle_ids.entry(TypeId::of::()).or_insert_with(|| { - let component_id = T::component_id(components); + let component_ids = T::component_ids(components); let id = BundleId(bundle_infos.len()); // SAFE: T::component_id ensures info was created let bundle_info = unsafe { - initialize_bundle(std::any::type_name::(), &component_id, id, components) + initialize_bundle(std::any::type_name::(), component_ids, id, components) }; bundle_infos.push(bundle_info); id @@ -225,17 +225,15 @@ impl Bundles { /// `component_id` must be valid [ComponentId]'s unsafe fn initialize_bundle( bundle_type_name: &'static str, - component_id: &[ComponentId], + component_ids: Vec, id: BundleId, components: &mut Components, ) -> BundleInfo { - let mut component_ids = Vec::new(); let mut storage_types = Vec::new(); - for &component_id in component_id { + for &component_id in &component_ids { // SAFE: component_id exists and is therefore valid let component_info = components.get_info_unchecked(component_id); - component_ids.push(component_id); storage_types.push(component_info.storage_type()); } diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 75aeb3ca5b1122..fa50ae8efd22af 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -115,7 +115,7 @@ mod tests { .unwrap(); assert_eq!( - ::component_id(world.components_mut()), + ::component_ids(world.components_mut()), vec![ world.components_mut().get_or_insert_id::<&'static str>(), world.components_mut().get_or_insert_id::(), @@ -151,7 +151,7 @@ mod tests { } assert_eq!( - ::component_id(world.components_mut()), + ::component_ids(world.components_mut()), vec![ world.components_mut().get_or_insert_id::(), world.components_mut().get_or_insert_id::<&'static str>(),