Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cbournhonesque-sc committed Jan 27, 2023
1 parent 2a68dbd commit e71b472
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 156 deletions.
60 changes: 29 additions & 31 deletions crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::container_attributes::ReflectTraits;
use crate::field_attributes::{parse_field_attrs, ReflectFieldAttr};
use crate::utility::members_to_serialization_denylist;
use crate::fq_std::{FQAny, FQDefault, FQSend, FQSync};
use crate::utility::{members_to_serialization_denylist, WhereClauseOptions};
use bit_set::BitSet;
use proc_macro2::TokenStream;
use quote::quote;

use crate::{utility, REFLECT_ATTRIBUTE_NAME, REFLECT_VALUE_ATTRIBUTE_NAME};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{Data, DeriveInput, Field, Fields, Generics, Ident, Meta, Path, Token, Type, Variant};
use syn::{Data, DeriveInput, Field, Fields, Generics, Ident, Meta, Path, Token, Variant};

pub(crate) enum ReflectDerive<'a> {
Struct(ReflectStruct<'a>),
Expand Down Expand Up @@ -317,27 +317,16 @@ impl<'a> ReflectMeta<'a> {
}

/// Returns the `GetTypeRegistration` impl as a `TokenStream`.
///
/// * `active_types`: types corresponding to active fields in the object (used to add specific trait bounds)
/// * `ignored_types`: types corresponding to ignored fields in the object (used to add specific trait bounds)
/// * `active_trait_bounds`: trait bounds to provide for the active types
/// * `ignored_trait_bounds`: trait bounds to provide for the ignored types
pub fn get_type_registration(
&self,
active_types: &[Type],
ignored_types: &[Type],
active_trait_bounds: &TokenStream,
ignored_trait_bounds: &TokenStream,
where_clause_options: &WhereClauseOptions,
) -> proc_macro2::TokenStream {
crate::registration::impl_get_type_registration(
self.type_name,
&self.bevy_reflect_path,
self.traits.idents(),
self.generics,
active_types,
ignored_types,
active_trait_bounds,
ignored_trait_bounds,
where_clause_options,
None,
)
}
Expand Down Expand Up @@ -366,17 +355,9 @@ impl<'a> ReflectStruct<'a> {
/// Returns the `GetTypeRegistration` impl as a `TokenStream`.
///
/// Returns a specific implementation for structs and this method should be preferred over the generic [`get_type_registration`](crate::ReflectMeta) method
///
/// * `active_types`: types corresponding to active fields in the struct (used to add specific trait bounds)
/// * `ignored_types`: types corresponding to ignored fields in the struct (used to add specific trait bounds)
/// * `active_trait_bounds`: trait bounds to provide for the active types
/// * `ignored_trait_bounds`: trait bounds to provide for the ignored types
pub fn get_type_registration(
&self,
active_types: &[Type],
ignored_types: &[Type],
active_trait_bounds: &TokenStream,
ignored_trait_bounds: &TokenStream,
where_clause_options: &WhereClauseOptions,
) -> proc_macro2::TokenStream {
let reflect_path = self.meta.bevy_reflect_path();

Expand All @@ -385,10 +366,7 @@ impl<'a> ReflectStruct<'a> {
reflect_path,
self.meta.traits().idents(),
self.meta.generics(),
active_types,
ignored_types,
active_trait_bounds,
ignored_trait_bounds,
where_clause_options,
Some(&self.serialization_denylist),
)
}
Expand Down Expand Up @@ -426,6 +404,16 @@ impl<'a> ReflectStruct<'a> {
pub fn fields(&self) -> &[StructField<'a>] {
&self.fields
}

pub fn where_clause_options(&self) -> WhereClauseOptions {
let bevy_reflect_path = &self.meta().bevy_reflect_path;
WhereClauseOptions {
active_types: self.active_types().into(),
active_trait_bounds: quote! { #bevy_reflect_path::Reflect },
ignored_types: self.ignored_types().into(),
ignored_trait_bounds: quote! { #FQAny + #FQSend + #FQSync },
}
}
}

impl<'a> ReflectEnum<'a> {
Expand Down Expand Up @@ -474,6 +462,16 @@ impl<'a> ReflectEnum<'a> {
.map(|field| field.data.ty.clone())
.collect()
}

pub fn where_clause_options(&self) -> WhereClauseOptions {
let bevy_reflect_path = &self.meta().bevy_reflect_path;
WhereClauseOptions {
active_types: self.active_types().into(),
active_trait_bounds: quote! { #bevy_reflect_path::FromReflect },
ignored_types: self.ignored_types().into(),
ignored_trait_bounds: quote! { #FQAny + #FQSend + #FQSync + #FQDefault },
}
}
}

impl<'a> EnumVariant<'a> {
Expand All @@ -482,15 +480,15 @@ impl<'a> EnumVariant<'a> {
pub fn active_fields(&self) -> impl Iterator<Item = &StructField<'a>> {
self.fields()
.iter()
.filter(move |field| field.attrs.ignore.is_active())
.filter(|field| field.attrs.ignore.is_active())
}

/// Get an iterator of fields which are ignored by the reflection API
#[allow(dead_code)]
pub fn ignored_fields(&self) -> impl Iterator<Item = &StructField<'a>> {
self.fields()
.iter()
.filter(move |field| field.attrs.ignore.is_ignored())
.filter(|field| field.attrs.ignore.is_ignored())
}

/// The complete set of fields in this variant.
Expand Down
29 changes: 7 additions & 22 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::derive_data::{EnumVariant, EnumVariantFields, ReflectEnum, StructField};
use crate::enum_utility::{get_variant_constructors, EnumVariantConstructors};
use crate::fq_std::{FQAny, FQBox, FQOption, FQResult, FQSend, FQSync};
use crate::fq_std::{FQAny, FQBox, FQOption, FQResult};
use crate::impls::impl_typed;
use crate::utility::extend_where_clause;
use proc_macro::TokenStream;
Expand All @@ -16,10 +16,7 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
let ref_index = Ident::new("__index_param", Span::call_site());
let ref_value = Ident::new("__value_param", Span::call_site());

let field_types = reflect_enum.active_types();
let ignored_types = reflect_enum.ignored_types();
let active_trait_bounds = quote! { #bevy_reflect_path::FromReflect };
let ignored_trait_bounds = quote! { #FQAny + #FQSend + #FQSync + Default };
let where_clause_options = reflect_enum.where_clause_options();

let EnumImpls {
variant_info,
Expand Down Expand Up @@ -82,10 +79,7 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
let typed_impl = impl_typed(
enum_name,
reflect_enum.meta().generics(),
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
&where_clause_options,
quote! {
let variants = [#(#variant_info),*];
let info = #info_generator;
Expand All @@ -94,22 +88,13 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> TokenStream {
bevy_reflect_path,
);

let get_type_registration_impl = reflect_enum.meta().get_type_registration(
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
);
let get_type_registration_impl = reflect_enum
.meta()
.get_type_registration(&where_clause_options);
let (impl_generics, ty_generics, where_clause) =
reflect_enum.meta().generics().split_for_impl();

let where_reflect_clause = extend_where_clause(
where_clause,
&field_types,
&active_trait_bounds,
&ignored_types,
&ignored_trait_bounds,
);
let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

TokenStream::from(quote! {
#get_type_registration_impl
Expand Down
26 changes: 5 additions & 21 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult, FQSend, FQSync};
use crate::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult};
use crate::impls::impl_typed;
use crate::utility::extend_where_clause;
use crate::ReflectStruct;
Expand Down Expand Up @@ -36,9 +36,6 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
})
.collect::<Vec<_>>();
let field_types = reflect_struct.active_types();
let ignored_types = reflect_struct.ignored_types();
let active_trait_bounds = quote! { #bevy_reflect_path::Reflect };
let ignored_trait_bounds = quote! { #FQAny + #FQSend + #FQSync };
let field_count = field_idents.len();
let field_indices = (0..field_count).collect::<Vec<usize>>();

Expand Down Expand Up @@ -92,13 +89,11 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
}
};

let where_clause_options = reflect_struct.where_clause_options();
let typed_impl = impl_typed(
struct_name,
reflect_struct.meta().generics(),
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
&where_clause_options,
quote! {
let fields = [#field_generator];
let info = #info_generator;
Expand All @@ -107,22 +102,11 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> TokenStream {
bevy_reflect_path,
);

let get_type_registration_impl = reflect_struct.get_type_registration(
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
);
let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);
let (impl_generics, ty_generics, where_clause) =
reflect_struct.meta().generics().split_for_impl();

let where_reflect_clause = extend_where_clause(
where_clause,
&field_types,
&active_trait_bounds,
&ignored_types,
&ignored_trait_bounds,
);
let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

TokenStream::from(quote! {
#get_type_registration_impl
Expand Down
26 changes: 5 additions & 21 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/tuple_structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult, FQSend, FQSync};
use crate::fq_std::{FQAny, FQBox, FQDefault, FQOption, FQResult};
use crate::impls::impl_typed;
use crate::utility::extend_where_clause;
use crate::ReflectStruct;
Expand All @@ -18,18 +18,11 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
.map(|field| Member::Unnamed(Index::from(field.index)))
.collect::<Vec<_>>();
let field_types = reflect_struct.active_types();
let ignored_types = reflect_struct.ignored_types();
let active_trait_bounds = quote! { #bevy_reflect_path::Reflect };
let ignored_trait_bounds = quote! { #FQAny + #FQSend + #FQSync };
let field_count = field_idents.len();
let field_indices = (0..field_count).collect::<Vec<usize>>();

let get_type_registration_impl = reflect_struct.get_type_registration(
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
);
let where_clause_options = reflect_struct.where_clause_options();
let get_type_registration_impl = reflect_struct.get_type_registration(&where_clause_options);

let hash_fn = reflect_struct
.meta()
Expand Down Expand Up @@ -85,10 +78,7 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
let typed_impl = impl_typed(
struct_name,
reflect_struct.meta().generics(),
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
&where_clause_options,
quote! {
let fields = [#field_generator];
let info = #info_generator;
Expand All @@ -100,13 +90,7 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> TokenStream {
let (impl_generics, ty_generics, where_clause) =
reflect_struct.meta().generics().split_for_impl();

let where_reflect_clause = extend_where_clause(
where_clause,
&field_types,
&active_trait_bounds,
&ignored_types,
&ignored_trait_bounds,
);
let where_reflect_clause = extend_where_clause(where_clause, &where_clause_options);

TokenStream::from(quote! {
#get_type_registration_impl
Expand Down
23 changes: 7 additions & 16 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use crate::utility::extend_where_clause;
use proc_macro2::{Ident, TokenStream};
use crate::utility::{extend_where_clause, WhereClauseOptions};
use proc_macro2::Ident;
use quote::quote;
use syn::{Generics, Path, Type};
use syn::{Generics, Path};

#[allow(clippy::too_many_arguments)]
pub(crate) fn impl_typed(
type_name: &Ident,
generics: &Generics,
active_types: &[Type],
ignored_types: &[Type],
active_trait_bounds: &TokenStream,
ignored_trait_bounds: &TokenStream,
generator: TokenStream,
where_clause_options: &WhereClauseOptions,
generator: proc_macro2::TokenStream,
bevy_reflect_path: &Path,
) -> TokenStream {
) -> proc_macro2::TokenStream {
let is_generic = !generics.params.is_empty();

let static_generator = if is_generic {
Expand All @@ -35,13 +32,7 @@ pub(crate) fn impl_typed(
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

// Add Typed bound for each active field
let where_reflect_clause = extend_where_clause(
where_clause,
active_types,
active_trait_bounds,
ignored_types,
ignored_trait_bounds,
);
let where_reflect_clause = extend_where_clause(where_clause, where_clause_options);

quote! {
impl #impl_generics #bevy_reflect_path::Typed for #type_name #ty_generics #where_reflect_clause {
Expand Down
18 changes: 4 additions & 14 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::fq_std::{FQAny, FQBox, FQClone, FQOption, FQResult};
use crate::impls::impl_typed;
use crate::utility::WhereClauseOptions;
use crate::ReflectMeta;
use proc_macro::TokenStream;
use quote::quote;
Expand All @@ -21,17 +22,11 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream {
#[cfg(not(feature = "documentation"))]
let with_docs: Option<proc_macro2::TokenStream> = None;

let field_types = Vec::default();
let ignored_types = Vec::default();
let active_trait_bounds = quote! {};
let ignored_trait_bounds = quote! {};
let where_clause_options = WhereClauseOptions::default();
let typed_impl = impl_typed(
type_name,
meta.generics(),
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
&where_clause_options,
quote! {
let info = #bevy_reflect_path::ValueInfo::new::<Self>() #with_docs;
#bevy_reflect_path::TypeInfo::Value(info)
Expand All @@ -40,12 +35,7 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> TokenStream {
);

let (impl_generics, ty_generics, where_clause) = meta.generics().split_for_impl();
let get_type_registration_impl = meta.get_type_registration(
&field_types,
&ignored_types,
&active_trait_bounds,
&ignored_trait_bounds,
);
let get_type_registration_impl = meta.get_type_registration(&where_clause_options);

TokenStream::from(quote! {
#get_type_registration_impl
Expand Down
Loading

0 comments on commit e71b472

Please sign in to comment.