Skip to content

Commit ed1a5ab

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 33bed5d commit ed1a5ab

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
@@ -605,13 +605,11 @@ impl<'a> ReflectStruct<'a> {
605605
}
606606

607607
/// Get a collection of types which are exposed to the reflection API
608-
pub fn active_types(&self) -> Vec<Type> {
609-
// Collect via `IndexSet` to eliminate duplicate types.
608+
pub fn active_types(&self) -> IndexSet<Type> {
609+
// Collect into an `IndexSet` to eliminate duplicate types.
610610
self.active_fields()
611611
.map(|field| field.reflected_type().clone())
612612
.collect::<IndexSet<_>>()
613-
.into_iter()
614-
.collect::<Vec<_>>()
615613
}
616614

617615
/// Get an iterator of fields which are exposed to the reflection API.
@@ -634,7 +632,7 @@ impl<'a> ReflectStruct<'a> {
634632
}
635633

636634
pub fn where_clause_options(&self) -> WhereClauseOptions {
637-
WhereClauseOptions::new_with_fields(self.meta(), self.active_types().into_boxed_slice())
635+
WhereClauseOptions::new_with_types(self.meta(), self.active_types())
638636
}
639637

640638
/// Generates a `TokenStream` for `TypeInfo::Struct` or `TypeInfo::TupleStruct` construction.
@@ -841,13 +839,11 @@ impl<'a> ReflectEnum<'a> {
841839
}
842840

843841
/// Get a collection of types which are exposed to the reflection API
844-
pub fn active_types(&self) -> Vec<Type> {
845-
// Collect via `IndexSet` to eliminate duplicate types.
842+
pub fn active_types(&self) -> IndexSet<Type> {
843+
// Collect into an `IndexSet` to eliminate duplicate types.
846844
self.active_fields()
847845
.map(|field| field.reflected_type().clone())
848846
.collect::<IndexSet<_>>()
849-
.into_iter()
850-
.collect::<Vec<_>>()
851847
}
852848

853849
/// Get an iterator of fields which are exposed to the reflection API
@@ -856,7 +852,7 @@ impl<'a> ReflectEnum<'a> {
856852
}
857853

858854
pub fn where_clause_options(&self) -> WhereClauseOptions {
859-
WhereClauseOptions::new_with_fields(self.meta(), self.active_types().into_boxed_slice())
855+
WhereClauseOptions::new_with_types(self.meta(), self.active_types())
860856
}
861857

862858
/// 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, TokenTree};
45
use quote::{quote, ToTokens};
56
use syn::{punctuated::Punctuated, Ident, 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
pub fn meta(&self) -> &'a ReflectMeta<'b> {
@@ -207,7 +205,7 @@ impl<'a, 'b> WhereClauseOptions<'a, 'b> {
207205
false
208206
}
209207

210-
Some(self.active_fields.iter().filter_map(move |ty| {
208+
Some(self.active_types.iter().filter_map(move |ty| {
211209
// Field type bounds are only required if `ty` is generic. How to determine that?
212210
// Search `ty`s token stream for identifiers that match the identifiers from the
213211
// function's type params. E.g. if `T` and `U` are the type param identifiers and

0 commit comments

Comments
 (0)