diff --git a/crates/bevy_reflect/derive/src/derive_data.rs b/crates/bevy_reflect/derive/src/derive_data.rs index 614f357b37e56..d39c787c3fd55 100644 --- a/crates/bevy_reflect/derive/src/derive_data.rs +++ b/crates/bevy_reflect/derive/src/derive_data.rs @@ -607,13 +607,11 @@ impl<'a> ReflectStruct<'a> { } /// Get a collection of types which are exposed to the reflection API - pub fn active_types(&self) -> Vec { - // Collect via `IndexSet` to eliminate duplicate types. + pub fn active_types(&self) -> IndexSet { + // Collect into an `IndexSet` to eliminate duplicate types. self.active_fields() .map(|field| field.reflected_type().clone()) .collect::>() - .into_iter() - .collect::>() } /// Get an iterator of fields which are exposed to the reflection API. @@ -636,7 +634,7 @@ impl<'a> ReflectStruct<'a> { } pub fn where_clause_options(&self) -> WhereClauseOptions { - WhereClauseOptions::new_with_fields(self.meta(), self.active_types().into_boxed_slice()) + WhereClauseOptions::new_with_types(self.meta(), self.active_types()) } /// Generates a `TokenStream` for `TypeInfo::Struct` or `TypeInfo::TupleStruct` construction. @@ -854,13 +852,11 @@ impl<'a> ReflectEnum<'a> { } /// Get a collection of types which are exposed to the reflection API - pub fn active_types(&self) -> Vec { - // Collect via `IndexSet` to eliminate duplicate types. + pub fn active_types(&self) -> IndexSet { + // Collect into an `IndexSet` to eliminate duplicate types. self.active_fields() .map(|field| field.reflected_type().clone()) .collect::>() - .into_iter() - .collect::>() } /// Get an iterator of fields which are exposed to the reflection API @@ -869,7 +865,7 @@ impl<'a> ReflectEnum<'a> { } pub fn where_clause_options(&self) -> WhereClauseOptions { - WhereClauseOptions::new_with_fields(self.meta(), self.active_types().into_boxed_slice()) + WhereClauseOptions::new_with_types(self.meta(), self.active_types()) } /// Returns the `GetTypeRegistration` impl as a `TokenStream`. @@ -883,7 +879,7 @@ impl<'a> ReflectEnum<'a> { self.meta(), where_clause_options, None, - Some(self.active_fields().map(StructField::reflected_type)), + Some(self.active_types().iter()), ) } diff --git a/crates/bevy_reflect/derive/src/where_clause_options.rs b/crates/bevy_reflect/derive/src/where_clause_options.rs index 1551e008d017c..7f573c780512b 100644 --- a/crates/bevy_reflect/derive/src/where_clause_options.rs +++ b/crates/bevy_reflect/derive/src/where_clause_options.rs @@ -1,5 +1,6 @@ use crate::derive_data::ReflectMeta; use bevy_macro_utils::fq_std::{FQAny, FQSend, FQSync}; +use indexmap::IndexSet; use proc_macro2::TokenStream; use quote::{quote, ToTokens}; use syn::{punctuated::Punctuated, Token, Type, WhereClause}; @@ -7,22 +8,19 @@ use syn::{punctuated::Punctuated, Token, Type, WhereClause}; /// Options defining how to extend the `where` clause for reflection. pub(crate) struct WhereClauseOptions<'a, 'b> { meta: &'a ReflectMeta<'b>, - active_fields: Box<[Type]>, + active_types: IndexSet, } impl<'a, 'b> WhereClauseOptions<'a, 'b> { pub fn new(meta: &'a ReflectMeta<'b>) -> Self { Self { meta, - active_fields: Box::new([]), + active_types: IndexSet::new(), } } - pub fn new_with_fields(meta: &'a ReflectMeta<'b>, active_fields: Box<[Type]>) -> Self { - Self { - meta, - active_fields, - } + pub fn new_with_types(meta: &'a ReflectMeta<'b>, active_types: IndexSet) -> Self { + Self { meta, active_types } } /// Extends the `where` clause for a type with additional bounds needed for the reflection impls. @@ -157,7 +155,7 @@ impl<'a, 'b> WhereClauseOptions<'a, 'b> { // construct `NamedField` and `UnnamedField` instances for the `Typed` impl. // Likewise, `GetTypeRegistration` is always required for active fields since // they are used to register the type's dependencies. - Some(self.active_fields.iter().map(move |ty| { + Some(self.active_types.iter().map(move |ty| { quote!( #ty : #reflect_bound + #bevy_reflect_path::TypePath