Skip to content

Commit

Permalink
Remove AssociatedData
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Oct 21, 2023
1 parent c489a5b commit 9effa6f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 152 deletions.
67 changes: 0 additions & 67 deletions crates/bevy_reflect/bevy_reflect_derive/src/associated_data.rs

This file was deleted.

57 changes: 4 additions & 53 deletions crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::associated_data::AssociatedData;
use crate::container_attributes::{FromReflectAttrs, ReflectTraits};
use crate::field_attributes::{parse_field_attrs, ReflectFieldAttr};
use crate::type_path::parse_path_no_leading_colon;
Expand Down Expand Up @@ -95,8 +94,6 @@ pub(crate) struct StructField<'a> {
pub data: &'a Field,
/// The reflection-based attributes on the field.
pub attrs: ReflectFieldAttr,
/// The associated data to be generated on behalf of this field.
pub associated_data: AssociatedData,
/// The index of this field within the struct.
pub declaration_index: usize,
/// The index of this field as seen by the reflection API.
Expand Down Expand Up @@ -279,13 +276,7 @@ impl<'a> ReflectDerive<'a> {

return match &input.data {
Data::Struct(data) => {
let fields = Self::collect_struct_fields(
&data.fields,
meta.type_path()
.get_ident()
.expect("structs should never be anonymous"),
meta.bevy_reflect_path(),
)?;
let fields = Self::collect_struct_fields(&data.fields)?;
let reflect_struct = ReflectStruct {
meta,
serialization_data: SerializationDataDef::new(&fields)?,
Expand All @@ -299,8 +290,7 @@ impl<'a> ReflectDerive<'a> {
}
}
Data::Enum(data) => {
let variants =
Self::collect_enum_variants(&data.variants, meta.bevy_reflect_path())?;
let variants = Self::collect_enum_variants(&data.variants)?;

let reflect_enum = ReflectEnum { meta, variants };
Ok(Self::Enum(reflect_enum))
Expand All @@ -322,25 +312,14 @@ impl<'a> ReflectDerive<'a> {
}
}

fn collect_struct_fields(
fields: &'a Fields,
qualifier: &Ident,
bevy_reflect_path: &Path,
) -> Result<Vec<StructField<'a>>, syn::Error> {
fn collect_struct_fields(fields: &'a Fields) -> Result<Vec<StructField<'a>>, syn::Error> {
let mut active_index = 0;
let sifter: utility::ResultSifter<StructField<'a>> = fields
.iter()
.enumerate()
.map(
|(declaration_index, field)| -> Result<StructField, syn::Error> {
let attrs = parse_field_attrs(&field.attrs)?;
let associated_data = AssociatedData::new(
field,
declaration_index,
&attrs,
qualifier,
bevy_reflect_path,
);

let reflection_index = if attrs.ignore.is_ignored() {
None
Expand All @@ -353,7 +332,6 @@ impl<'a> ReflectDerive<'a> {
declaration_index,
reflection_index,
attrs,
associated_data,
data: field,
#[cfg(feature = "documentation")]
doc: crate::documentation::Documentation::from_attributes(&field.attrs),
Expand All @@ -370,17 +348,12 @@ impl<'a> ReflectDerive<'a> {

fn collect_enum_variants(
variants: &'a Punctuated<Variant, Comma>,
bevy_reflect_path: &Path,
) -> Result<Vec<EnumVariant<'a>>, syn::Error> {
let sifter: utility::ResultSifter<EnumVariant<'a>> = variants
.iter()
.enumerate()
.map(|(index, variant)| -> Result<EnumVariant, syn::Error> {
let fields = Self::collect_struct_fields(
&variant.fields,
&variant.ident,
bevy_reflect_path,
)?;
let fields = Self::collect_struct_fields(&variant.fields)?;

let fields = match variant.fields {
Fields::Named(..) => EnumVariantFields::Named(fields),
Expand All @@ -403,23 +376,6 @@ impl<'a> ReflectDerive<'a> {

sifter.finish()
}

/// The complete set of fields in this item.
pub fn fields(&self) -> Box<dyn Iterator<Item = &StructField<'a>> + '_> {
match self {
ReflectDerive::Struct(reflect_struct)
| ReflectDerive::TupleStruct(reflect_struct)
| ReflectDerive::UnitStruct(reflect_struct) => Box::new(reflect_struct.fields.iter()),
ReflectDerive::Enum(reflect_enum) => Box::new(reflect_enum.fields()),
ReflectDerive::Value(_) => Box::new(core::iter::empty()),
}
}

/// Generate all [associated data](AssociatedData) into a `TokenStream`.
pub fn associated_data(&self) -> proc_macro2::TokenStream {
let associated_data = self.fields().map(|field| &field.associated_data);
quote!(#(#associated_data)*)
}
}

impl<'a> ReflectMeta<'a> {
Expand Down Expand Up @@ -551,11 +507,6 @@ impl<'a> ReflectEnum<'a> {
&self.variants
}

/// The complete set of fields in this enum.
pub fn fields(&self) -> impl Iterator<Item = &StructField<'a>> {
self.variants().iter().flat_map(|variant| variant.fields())
}

/// Get an iterator of fields which are exposed to the reflection API
pub fn active_fields(&self) -> impl Iterator<Item = &StructField<'a>> {
self.variants()
Expand Down
4 changes: 0 additions & 4 deletions crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

extern crate proc_macro;

mod associated_data;
mod container_attributes;
mod derive_data;
#[cfg(feature = "documentation")]
Expand Down Expand Up @@ -167,8 +166,6 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream {
Err(err) => return err.into_compile_error().into(),
};

let associated_data = derive_data.associated_data();

let (reflect_impls, from_reflect_impl) = match derive_data {
ReflectDerive::Struct(struct_data) | ReflectDerive::UnitStruct(struct_data) => (
impls::impl_struct(&struct_data),
Expand Down Expand Up @@ -206,7 +203,6 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream {

TokenStream::from(quote! {
const _: () = {
#associated_data
#reflect_impls
#from_reflect_impl
};
Expand Down
45 changes: 17 additions & 28 deletions crates/bevy_reflect/bevy_reflect_derive/src/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::derive_data::StructField;
use crate::field_attributes::ReflectIgnoreBehavior;
use proc_macro2::Ident;
use bevy_macro_utils::fq_std::{FQBox, FQDefault};
use quote::quote;
use std::collections::HashMap;
use syn::spanned::Spanned;
Expand Down Expand Up @@ -48,15 +48,15 @@ impl SerializationDataDef {

/// Returns a `TokenStream` containing an initialized `SerializationData` type.
pub fn as_serialization_data(&self, bevy_reflect_path: &Path) -> proc_macro2::TokenStream {
let fields = self.skipped.iter().map(|(reflection_index, data)| {
let SkippedFieldDef {
associated_default_fn: default_fn,
} = data;
quote! {(
#reflection_index,
#bevy_reflect_path::serde::SkippedField::new(#default_fn)
)}
});
let fields =
self.skipped
.iter()
.map(|(reflection_index, SkippedFieldDef { default_fn })| {
quote! {(
#reflection_index,
#bevy_reflect_path::serde::SkippedField::new(#default_fn)
)}
});
quote! {
#bevy_reflect_path::serde::SerializationData::new(
::core::iter::IntoIterator::into_iter([#(#fields),*])
Expand All @@ -67,30 +67,19 @@ impl SerializationDataDef {

/// Collected field data used to generate a `SkippedField` type.
pub(crate) struct SkippedFieldDef {
/// The identifier of the [default function] generated by [`AssociatedData`].
/// The default function for this field.
///
/// [default function]: crate::associated_data::AssociatedData::default_fn
/// [`AssociatedData`]: crate::associated_data::AssociatedData
associated_default_fn: Ident,
/// This is of type `fn() -> Box<dyn Reflect>`.
default_fn: proc_macro2::TokenStream,
}

impl SkippedFieldDef {
pub fn new(field: &StructField<'_>) -> Result<Self, syn::Error> {
let associated_default_fn = field
.associated_data
.default_fn()
.ok_or_else(|| {
syn::Error::new(
field.data.span(),
"internal error: field is missing an associated default function",
)
})?
.sig
.ident
.clone();

let ty = &field.data.ty;
Ok(Self {
associated_default_fn,
default_fn: quote! {
|| { #FQBox::new(<#ty as #FQDefault>::default()) }
},
})
}
}

0 comments on commit 9effa6f

Please sign in to comment.