Skip to content

Commit d102dbe

Browse files
committed
Change WhereClauseOptions::active_fields to active_types
This could have been part of #19876, which deduplicated the fields. It's also simpler and more efficient to keep the (short-lived) active types as an `IndexSet<Type>`, and avoid converting them to a `Vec<Type>` and then a `Box<[Type]>`.
1 parent 8351da4 commit d102dbe

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

crates/bevy_reflect/derive/src/derive_data.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -607,13 +607,11 @@ impl<'a> ReflectStruct<'a> {
607607
}
608608

609609
/// Get a collection of types which are exposed to the reflection API
610-
pub fn active_types(&self) -> Vec<Type> {
611-
// Collect via `IndexSet` to eliminate duplicate types.
610+
pub fn active_types(&self) -> IndexSet<Type> {
611+
// Collect into an `IndexSet` to eliminate duplicate types.
612612
self.active_fields()
613613
.map(|field| field.reflected_type().clone())
614614
.collect::<IndexSet<_>>()
615-
.into_iter()
616-
.collect::<Vec<_>>()
617615
}
618616

619617
/// Get an iterator of fields which are exposed to the reflection API.
@@ -636,7 +634,7 @@ impl<'a> ReflectStruct<'a> {
636634
}
637635

638636
pub fn where_clause_options(&self) -> WhereClauseOptions {
639-
WhereClauseOptions::new_with_fields(self.meta(), self.active_types().into_boxed_slice())
637+
WhereClauseOptions::new_with_types(self.meta(), self.active_types())
640638
}
641639

642640
/// Generates a `TokenStream` for `TypeInfo::Struct` or `TypeInfo::TupleStruct` construction.
@@ -854,13 +852,11 @@ impl<'a> ReflectEnum<'a> {
854852
}
855853

856854
/// Get a collection of types which are exposed to the reflection API
857-
pub fn active_types(&self) -> Vec<Type> {
858-
// Collect via `IndexSet` to eliminate duplicate types.
855+
pub fn active_types(&self) -> IndexSet<Type> {
856+
// Collect into an `IndexSet` to eliminate duplicate types.
859857
self.active_fields()
860858
.map(|field| field.reflected_type().clone())
861859
.collect::<IndexSet<_>>()
862-
.into_iter()
863-
.collect::<Vec<_>>()
864860
}
865861

866862
/// Get an iterator of fields which are exposed to the reflection API
@@ -869,7 +865,7 @@ impl<'a> ReflectEnum<'a> {
869865
}
870866

871867
pub fn where_clause_options(&self) -> WhereClauseOptions {
872-
WhereClauseOptions::new_with_fields(self.meta(), self.active_types().into_boxed_slice())
868+
WhereClauseOptions::new_with_types(self.meta(), self.active_types())
873869
}
874870

875871
/// Returns the `GetTypeRegistration` impl as a `TokenStream`.

crates/bevy_reflect/derive/src/where_clause_options.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
use crate::derive_data::ReflectMeta;
22
use bevy_macro_utils::fq_std::{FQAny, FQSend, FQSync};
3+
use indexmap::IndexSet;
34
use proc_macro2::TokenStream;
45
use quote::{quote, ToTokens};
56
use syn::{punctuated::Punctuated, Token, Type, WhereClause};
67

78
/// Options defining how to extend the `where` clause for reflection.
89
pub(crate) struct WhereClauseOptions<'a, 'b> {
910
meta: &'a ReflectMeta<'b>,
10-
active_fields: Box<[Type]>,
11+
active_types: IndexSet<Type>,
1112
}
1213

1314
impl<'a, 'b> WhereClauseOptions<'a, 'b> {
1415
pub fn new(meta: &'a ReflectMeta<'b>) -> Self {
1516
Self {
1617
meta,
17-
active_fields: Box::new([]),
18+
active_types: IndexSet::new(),
1819
}
1920
}
2021

21-
pub fn new_with_fields(meta: &'a ReflectMeta<'b>, active_fields: Box<[Type]>) -> Self {
22-
Self {
23-
meta,
24-
active_fields,
25-
}
22+
pub fn new_with_types(meta: &'a ReflectMeta<'b>, active_types: IndexSet<Type>) -> Self {
23+
Self { meta, active_types }
2624
}
2725

2826
/// 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> {
157155
// construct `NamedField` and `UnnamedField` instances for the `Typed` impl.
158156
// Likewise, `GetTypeRegistration` is always required for active fields since
159157
// they are used to register the type's dependencies.
160-
Some(self.active_fields.iter().map(move |ty| {
158+
Some(self.active_types.iter().map(move |ty| {
161159
quote!(
162160
#ty : #reflect_bound
163161
+ #bevy_reflect_path::TypePath

0 commit comments

Comments
 (0)