Skip to content
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

[Merged by Bors] - Add reflect(skip_serializing) which retains reflection but disables automatic serialization #5250

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
rebase changes
  • Loading branch information
makspll committed Sep 15, 2022
commit e249d97fe4aafb95517a8cb235b48de0350ce9ee
1 change: 1 addition & 0 deletions crates/bevy_reflect/bevy_reflect_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ syn = { version = "1.0", features = ["full"] }
proc-macro2 = "1.0"
quote = "1.0"
uuid = { version = "1.1", features = ["v4"] }
bit-set = "0.5.2"
87 changes: 41 additions & 46 deletions crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use std::iter::empty;

use crate::container_attributes::ReflectTraits;
use crate::field_attributes::{parse_field_attrs, ReflectFieldAttr, ReflectFieldIgnore};
use crate::field_attributes::{parse_field_attrs, ReflectFieldAttr, ReflectIgnoreBehaviour};
use crate::utility::members_to_serialization_blacklist;
use bit_set::BitSet;
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 @@ -37,6 +41,8 @@ pub(crate) struct ReflectMeta<'a> {
generics: &'a Generics,
/// A cached instance of the path to the `bevy_reflect` crate.
bevy_reflect_path: Path,
/// A collection corresponding to `ignored` fields' indices during serialization.
serialization_blacklist: BitSet<u32>,
makspll marked this conversation as resolved.
Show resolved Hide resolved
}

/// Struct data used by derive macros for `Reflect` and `FromReflect`.
Expand Down Expand Up @@ -92,6 +98,7 @@ pub(crate) struct EnumVariant<'a> {
/// The fields within this variant.
pub fields: EnumVariantFields<'a>,
/// The reflection-based attributes on the variant.
#[allow(dead_code)]
makspll marked this conversation as resolved.
Show resolved Hide resolved
pub attrs: ReflectFieldAttr,
/// The index of this variant within the enum.
#[allow(dead_code)]
Expand Down Expand Up @@ -125,19 +132,25 @@ impl<'a> ReflectDerive<'a> {
_ => continue,
}
}

let meta = ReflectMeta::new(&input.ident, &input.generics, traits);

if force_reflect_value {
return Ok(Self::Value(meta));
return Ok(Self::Value(ReflectMeta::new(
&input.ident,
&input.generics,
traits,
empty(),
)));
}

return match &input.data {
Data::Struct(data) => {
let reflect_struct = ReflectStruct {
meta,
fields: Self::collect_struct_fields(&data.fields)?,
};
let fields = Self::collect_struct_fields(&data.fields)?;
let meta = ReflectMeta::new(
&input.ident,
&input.generics,
traits,
fields.iter().map(|f| f.attrs.ignore),
);
let reflect_struct = ReflectStruct { meta, fields };

match data.fields {
Fields::Named(..) => Ok(Self::Struct(reflect_struct)),
Expand All @@ -146,10 +159,10 @@ impl<'a> ReflectDerive<'a> {
}
}
Data::Enum(data) => {
let reflect_enum = ReflectEnum {
meta,
variants: Self::collect_enum_variants(&data.variants)?,
};
let variants = Self::collect_enum_variants(&data.variants)?;
let meta = ReflectMeta::new(&input.ident, &input.generics, traits, empty());

let reflect_enum = ReflectEnum { meta, variants };
Ok(Self::Enum(reflect_enum))
}
Data::Union(..) => Err(syn::Error::new(
Expand Down Expand Up @@ -210,12 +223,18 @@ impl<'a> ReflectDerive<'a> {
}

impl<'a> ReflectMeta<'a> {
pub fn new(type_name: &'a Ident, generics: &'a Generics, traits: ReflectTraits) -> Self {
pub fn new<I: Iterator<Item = ReflectIgnoreBehaviour>>(
type_name: &'a Ident,
generics: &'a Generics,
traits: ReflectTraits,
member_meta_iter: I,
) -> Self {
Self {
traits,
type_name,
generics,
bevy_reflect_path: utility::get_bevy_reflect_path(),
serialization_blacklist: members_to_serialization_blacklist(member_meta_iter),
}
}

Expand All @@ -241,25 +260,12 @@ impl<'a> ReflectMeta<'a> {

/// Returns the `GetTypeRegistration` impl as a `TokenStream`.
pub fn get_type_registration(&self) -> proc_macro2::TokenStream {
let mut idxs = Vec::default();
self.fields.iter().fold(0, |next_idx, field| {
if field.attrs.ignore == ReflectFieldIgnore::IgnoreAlways {
idxs.push(next_idx);
next_idx
} else if field.attrs.ignore == ReflectFieldIgnore::IgnoreSerialization {
idxs.push(next_idx);
next_idx + 1
} else {
next_idx + 1
}
});

crate::registration::impl_get_type_registration(
self.type_name,
&self.bevy_reflect_path,
self.traits.idents(),
self.generics,
&idxs,
&self.serialization_blacklist,
)
}
}
Expand All @@ -285,19 +291,19 @@ impl<'a> ReflectStruct<'a> {
.collect::<Vec<_>>()
}

/// Get a collection of types which are exposed to either the scene serialization or reflection API
/// Get a collection of types which are exposed to either the serialization or reflection API
pub fn active_types(&self) -> Vec<syn::Type> {
self.types_with(|field| field.attrs.ignore != ReflectFieldIgnore::IgnoreAlways)
self.types_with(|field| field.attrs.ignore.is_active())
}

/// Get a collection of fields which are exposed to the reflection API
/// Get an iterator of fields which are exposed to the serialization or reflection API
makspll marked this conversation as resolved.
Show resolved Hide resolved
pub fn active_fields(&self) -> impl Iterator<Item = &StructField<'a>> {
self.fields_with(|field| field.attrs.ignore != ReflectFieldIgnore::IgnoreAlways)
self.fields_with(|field| field.attrs.ignore.is_active())
}

/// Get a collection of fields which are ignored from the reflection API
/// Get an iterator of fields which are ignored by the reflection and serialization API
pub fn ignored_fields(&self) -> impl Iterator<Item = &StructField<'a>> {
self.fields_with(|field| field.attrs.ignore != ReflectFieldIgnore::None)
self.fields_with(|field| field.attrs.ignore.is_ignored())
}

/// The complete set of fields in this struct.
Expand All @@ -313,17 +319,6 @@ impl<'a> ReflectEnum<'a> {
&self.meta
}

/// Get an iterator over the active variants.
pub fn active_variants(&self) -> impl Iterator<Item = &EnumVariant<'a>> {
self.variants.iter().filter(|variant| !variant.attrs.ignore)
}

/// Get an iterator over the ignored variants.
#[allow(dead_code)]
pub fn ignored_variants(&self) -> impl Iterator<Item = &EnumVariant<'a>> {
self.variants.iter().filter(|variant| variant.attrs.ignore)
}

/// Returns the given ident as a qualified unit variant of this enum.
pub fn get_unit(&self, variant: &Ident) -> proc_macro2::TokenStream {
let name = self.meta.type_name;
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/enum_utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn get_variant_constructors(
let mut variant_names = Vec::with_capacity(variant_count);
let mut variant_constructors = Vec::with_capacity(variant_count);

for variant in reflect_enum.active_variants() {
for variant in reflect_enum.variants() {
let ident = &variant.data.ident;
let name = ident.to_string();
let variant_constructor = reflect_enum.get_unit(ident);
Expand All @@ -38,7 +38,7 @@ pub(crate) fn get_variant_constructors(
let mut reflect_index: usize = 0;
let constructor_fields = fields.iter().enumerate().map(|(declar_index, field)| {
let field_ident = ident_or_index(field.data.ident.as_ref(), declar_index);
let field_value = if field.attrs.ignore {
let field_value = if field.attrs.ignore.is_ignored() {
quote! { Default::default() }
} else {
let error_repr = field.data.ident.as_ref().map_or_else(
Expand Down
34 changes: 27 additions & 7 deletions crates/bevy_reflect/bevy_reflect_derive/src/field_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,42 @@ pub(crate) static IGNORE_ALL_ATTR: &str = "ignore";
pub(crate) static DEFAULT_ATTR: &str = "default";

/// Stores data about if the field should be visible via the Reflect and serialization interfaces
///
/// Note the relationship between serialization and reflection is such that a member must be reflected in order to be serialized.
/// In boolean logic this is described as: `is_serialized -> is_reflected`, this means we can reflect something without serializing it but not the other way round.
/// The `is_reflected` predicate is provided as `self.is_active()`
#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ReflectFieldIgnore {
pub(crate) enum ReflectIgnoreBehaviour {
makspll marked this conversation as resolved.
Show resolved Hide resolved
/// Don't ignore, appear to all systems
#[default]
None,
/// Ignore when serializing but not when reflecting
IgnoreSerialization,
/// Ignore for all systems
/// Ignore both when serializing and reflecting
IgnoreAlways,
}

impl ReflectIgnoreBehaviour {
/// Returns `true` if the ignoring behaviour implies member is included in the reflection API, and false otherwise.
pub fn is_active(self) -> bool {
match self {
ReflectIgnoreBehaviour::None => true,
ReflectIgnoreBehaviour::IgnoreSerialization => true,
ReflectIgnoreBehaviour::IgnoreAlways => false,
}
}

/// The exact logical opposite of `self.is_active()` returns true iff this member is not part of the reflection API whatsover (neither serialized nor reflected)
pub fn is_ignored(self) -> bool {
!self.is_active()
}
}

/// A container for attributes defined on a reflected type's field.
#[derive(Default)]
pub(crate) struct ReflectFieldAttr {
/// Determines how this field should be ignored if at all.
pub ignore: ReflectFieldIgnore,
pub ignore: ReflectIgnoreBehaviour,
/// Sets the default behavior of this field.
pub default: DefaultBehavior,
}
Expand Down Expand Up @@ -80,13 +100,13 @@ pub(crate) fn parse_field_attrs(attrs: &[Attribute]) -> Result<ReflectFieldAttr,
fn parse_meta(args: &mut ReflectFieldAttr, meta: &Meta) -> Result<(), syn::Error> {
match meta {
Meta::Path(path) if path.is_ident(IGNORE_SERIALIZATION_ATTR) => {
(args.ignore == ReflectFieldIgnore::None)
.then(|| args.ignore = ReflectFieldIgnore::IgnoreSerialization)
(args.ignore == ReflectIgnoreBehaviour::None)
.then(|| args.ignore = ReflectIgnoreBehaviour::IgnoreSerialization)
.ok_or_else(|| syn::Error::new_spanned(path, format!("Only one of ['{IGNORE_SERIALIZATION_ATTR}','{IGNORE_ALL_ATTR}'] is allowed")))
}
Meta::Path(path) if path.is_ident(IGNORE_ALL_ATTR) => {
(args.ignore == ReflectFieldIgnore::None)
.then(|| args.ignore = ReflectFieldIgnore::IgnoreAlways)
(args.ignore == ReflectIgnoreBehaviour::None)
.then(|| args.ignore = ReflectIgnoreBehaviour::IgnoreAlways)
.ok_or_else(|| syn::Error::new_spanned(path, format!("Only one of ['{IGNORE_SERIALIZATION_ATTR}','{IGNORE_ALL_ATTR}'] is allowed")))
}
Meta::Path(path) if path.is_ident(DEFAULT_ATTR) => {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
let mut enum_variant_name = Vec::new();
let mut enum_variant_type = Vec::new();

for variant in reflect_enum.active_variants() {
for variant in reflect_enum.variants() {
let ident = &variant.data.ident;
let name = ident.to_string();
let unit = reflect_enum.get_unit(ident);
Expand All @@ -281,7 +281,7 @@ fn generate_impls(reflect_enum: &ReflectEnum, ref_index: &Ident, ref_name: &Iden
let mut constructor_argument = Vec::new();
let mut reflect_idx = 0;
for field in fields.iter() {
if field.attrs.ignore {
if field.attrs.ignore.is_ignored() {
// Ignored field
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::derive_data::{ReflectDerive, ReflectMeta, ReflectStruct};
use proc_macro::TokenStream;
use quote::quote;
use reflect_value::ReflectValueDef;
use std::iter::empty;
use syn::spanned::Spanned;
use syn::{parse_macro_input, DeriveInput};

Expand Down Expand Up @@ -99,6 +100,7 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream {
&def.type_name,
&def.generics,
def.traits.unwrap_or_default(),
empty(),
))
}

Expand Down Expand Up @@ -175,5 +177,6 @@ pub fn impl_from_reflect_value(input: TokenStream) -> TokenStream {
&def.type_name,
&def.generics,
def.traits.unwrap_or_default(),
empty(),
))
}
6 changes: 4 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/registration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Contains code related specifically to Bevy's type registration.

use bit_set::BitSet;
use proc_macro2::Ident;
use quote::quote;
use syn::{Generics, Path};
Expand All @@ -10,17 +11,18 @@ pub(crate) fn impl_get_type_registration(
bevy_reflect_path: &Path,
registration_data: &[Ident],
generics: &Generics,
scene_ignored_field_indices: &[usize],
serialization_blacklist: &BitSet<u32>,
) -> proc_macro2::TokenStream {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let serialization_blacklist_indices = serialization_blacklist.iter().map(|v| v as usize);
quote! {
#[allow(unused_mut)]
impl #impl_generics #bevy_reflect_path::GetTypeRegistration for #type_name #ty_generics #where_clause {
fn get_type_registration() -> #bevy_reflect_path::TypeRegistration {
let mut registration = #bevy_reflect_path::TypeRegistration::of::<#type_name #ty_generics>();
registration.insert::<#bevy_reflect_path::ReflectFromPtr>(#bevy_reflect_path::FromType::<#type_name #ty_generics>::from_type());
#(registration.insert::<#registration_data>(#bevy_reflect_path::FromType::<#type_name #ty_generics>::from_type());)*
let serialization_data = #bevy_reflect_path::SerializationData::new([#(#scene_ignored_field_indices),*].into_iter());
let serialization_data = #bevy_reflect_path::SerializationData::new([#(#serialization_blacklist_indices),*].into_iter());
registration.set_serialization_data(serialization_data);
registration
}
Expand Down
41 changes: 41 additions & 0 deletions crates/bevy_reflect/bevy_reflect_derive/src/utility.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! General-purpose utility functions for internal usage within this crate.

use crate::field_attributes::ReflectIgnoreBehaviour;
use bevy_macro_utils::BevyManifest;
use bit_set::BitSet;
use proc_macro2::{Ident, Span};
use syn::{Member, Path};

Expand Down Expand Up @@ -96,3 +98,42 @@ impl<T> ResultSifter<T> {
}
}
}

/// Converts an iterator over ignore behaviour of members to a bitset of ignored members.
///
/// Takes into account the fact that always ignored (non-reflected) members do not count as members.
///
/// # Example
/// ```rust,ignore
/// pub struct HelloWorld {
/// reflected_field: u32 // index: 0
///
/// #[reflect(ignore)]
/// non_reflected_field: u32 // index: N/A (not 1!)
///
/// #[reflect(skip_serializing)]
/// non_serialized_field: u32 // index: 1
/// }
/// ```
/// Would convert to the `0b01` bitset (i.e second field is NOT serialized)
makspll marked this conversation as resolved.
Show resolved Hide resolved
///
pub(crate) fn members_to_serialization_blacklist<T>(member_iter: T) -> BitSet<u32>
where
T: Iterator<Item = ReflectIgnoreBehaviour>,
{
let mut bitset = BitSet::default();

member_iter.fold(0, |next_idx, member| {
if member == ReflectIgnoreBehaviour::IgnoreAlways {
bitset.insert(next_idx);
next_idx
} else if member == ReflectIgnoreBehaviour::IgnoreSerialization {
bitset.insert(next_idx);
next_idx + 1
} else {
next_idx + 1
}
makspll marked this conversation as resolved.
Show resolved Hide resolved
});

bitset
}
Loading