Skip to content

bevy_reflect: Use active_types instead of active_fields where appropriate #19922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions crates/bevy_reflect/derive/src/derive_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Type> {
// Collect via `IndexSet` to eliminate duplicate types.
pub fn active_types(&self) -> IndexSet<Type> {
// Collect into an `IndexSet` to eliminate duplicate types.
self.active_fields()
.map(|field| field.reflected_type().clone())
.collect::<IndexSet<_>>()
.into_iter()
.collect::<Vec<_>>()
}

/// Get an iterator of fields which are exposed to the reflection API.
Expand All @@ -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.
Expand Down Expand Up @@ -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<Type> {
// Collect via `IndexSet` to eliminate duplicate types.
pub fn active_types(&self) -> IndexSet<Type> {
// Collect into an `IndexSet` to eliminate duplicate types.
self.active_fields()
.map(|field| field.reflected_type().clone())
.collect::<IndexSet<_>>()
.into_iter()
.collect::<Vec<_>>()
}

/// Get an iterator of fields which are exposed to the reflection API
Expand All @@ -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`.
Expand All @@ -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()),
)
}

Expand Down
14 changes: 6 additions & 8 deletions crates/bevy_reflect/derive/src/where_clause_options.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
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};

/// 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<Type>,
}

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<Type>) -> Self {
Self { meta, active_types }
}

/// Extends the `where` clause for a type with additional bounds needed for the reflection impls.
Expand Down Expand Up @@ -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
Expand Down