Skip to content

Commit

Permalink
Rename ReflectTraits -> ContainerAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Feb 8, 2024
1 parent 68c456c commit 1472fc9
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 39 deletions.
Expand Up @@ -215,7 +215,7 @@ impl TypePathAttrs {
/// > __Note:__ Registering a custom function only works for special traits.
///
#[derive(Default, Clone)]
pub(crate) struct ReflectTraits {
pub(crate) struct ContainerAttributes {
debug: TraitImpl,
hash: TraitImpl,
partial_eq: TraitImpl,
Expand All @@ -226,7 +226,7 @@ pub(crate) struct ReflectTraits {
idents: Vec<Ident>,
}

impl ReflectTraits {
impl ContainerAttributes {
/// Parse a comma-separated list of container attributes.
///
/// # Example
Expand All @@ -241,7 +241,7 @@ impl ReflectTraits {
Ok(this)
}

/// Parse the contents of a `#[reflect(...)]` attribute into a [`ReflectTraits`] instance.
/// Parse the contents of a `#[reflect(...)]` attribute into a [`ContainerAttributes`] instance.
///
/// # Example
/// - `#[reflect(Hash, Debug(custom_debug), MyTrait)]`
Expand Down Expand Up @@ -531,10 +531,10 @@ impl ReflectTraits {
self.no_field_bounds
}

/// Merges the trait implementations of this [`ReflectTraits`] with another one.
/// Merges the trait implementations of this [`ContainerAttributes`] with another one.
///
/// An error is returned if the two [`ReflectTraits`] have conflicting implementations.
pub fn merge(&mut self, other: ReflectTraits) -> Result<(), syn::Error> {
/// An error is returned if the two [`ContainerAttributes`] have conflicting implementations.
pub fn merge(&mut self, other: ContainerAttributes) -> Result<(), syn::Error> {
// Destructuring is used to help ensure that all fields are merged
let Self {
debug,
Expand Down
24 changes: 13 additions & 11 deletions crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs
@@ -1,6 +1,6 @@
use core::fmt;

use crate::container_attributes::{FromReflectAttrs, ReflectTraits, TypePathAttrs};
use crate::container_attributes::{ContainerAttributes, FromReflectAttrs, TypePathAttrs};
use crate::field_attributes::FieldAttributes;
use crate::type_path::parse_path_no_leading_colon;
use crate::utility::{StringExpr, WhereClauseOptions};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub(crate) enum ReflectDerive<'a> {
/// ```
pub(crate) struct ReflectMeta<'a> {
/// The registered traits for this type.
traits: ReflectTraits,
attrs: ContainerAttributes,
/// The path to this type.
type_path: ReflectTypePath<'a>,
/// A cached instance of the path to the `bevy_reflect` crate.
Expand Down Expand Up @@ -183,7 +183,7 @@ impl<'a> ReflectDerive<'a> {
input: &'a DeriveInput,
provenance: ReflectProvenance,
) -> Result<Self, syn::Error> {
let mut traits = ReflectTraits::default();
let mut traits = ContainerAttributes::default();
// Should indicate whether `#[reflect_value]` was used.
let mut reflect_mode = None;
// Should indicate whether `#[type_path = "..."]` was used.
Expand All @@ -205,7 +205,8 @@ impl<'a> ReflectDerive<'a> {
}

reflect_mode = Some(ReflectMode::Normal);
let new_traits = ReflectTraits::parse_meta_list(meta_list, provenance.trait_)?;
let new_traits =
ContainerAttributes::parse_meta_list(meta_list, provenance.trait_)?;
traits.merge(new_traits)?;
}
Meta::List(meta_list) if meta_list.path.is_ident(REFLECT_VALUE_ATTRIBUTE_NAME) => {
Expand All @@ -217,7 +218,8 @@ impl<'a> ReflectDerive<'a> {
}

reflect_mode = Some(ReflectMode::Value);
let new_traits = ReflectTraits::parse_meta_list(meta_list, provenance.trait_)?;
let new_traits =
ContainerAttributes::parse_meta_list(meta_list, provenance.trait_)?;
traits.merge(new_traits)?;
}
Meta::Path(path) if path.is_ident(REFLECT_VALUE_ATTRIBUTE_NAME) => {
Expand Down Expand Up @@ -421,9 +423,9 @@ impl<'a> ReflectDerive<'a> {
}

impl<'a> ReflectMeta<'a> {
pub fn new(type_path: ReflectTypePath<'a>, traits: ReflectTraits) -> Self {
pub fn new(type_path: ReflectTypePath<'a>, attrs: ContainerAttributes) -> Self {
Self {
traits,
attrs,
type_path,
bevy_reflect_path: utility::get_bevy_reflect_path(),
#[cfg(feature = "documentation")]
Expand All @@ -438,19 +440,19 @@ impl<'a> ReflectMeta<'a> {
}

/// The registered reflect traits on this struct.
pub fn traits(&self) -> &ReflectTraits {
&self.traits
pub fn attrs(&self) -> &ContainerAttributes {
&self.attrs
}

/// The `FromReflect` attributes on this type.
#[allow(clippy::wrong_self_convention)]
pub fn from_reflect(&self) -> &FromReflectAttrs {
self.traits.from_reflect_attrs()
self.attrs.from_reflect_attrs()
}

/// The `TypePath` attributes on this type.
pub fn type_path_attrs(&self) -> &TypePathAttrs {
self.traits.type_path_attrs()
self.attrs.type_path_attrs()
}

/// The path to this type.
Expand Down
Expand Up @@ -98,7 +98,7 @@ fn impl_struct_internal(
let MemberValuePair(active_members, active_values) =
get_active_fields(reflect_struct, &ref_struct, &ref_struct_type, is_tuple);

let is_defaultable = reflect_struct.meta().traits().contains(REFLECT_DEFAULT);
let is_defaultable = reflect_struct.meta().attrs().contains(REFLECT_DEFAULT);
let constructor = if is_defaultable {
quote!(
let mut __this: Self = #FQDefault::default();
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/enums.rs
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream

let hash_fn = reflect_enum
.meta()
.traits()
.attrs()
.get_hash_impl(bevy_reflect_path)
.unwrap_or_else(|| {
quote! {
Expand All @@ -44,10 +44,10 @@ pub(crate) fn impl_enum(reflect_enum: &ReflectEnum) -> proc_macro2::TokenStream
}
}
});
let debug_fn = reflect_enum.meta().traits().get_debug_impl();
let debug_fn = reflect_enum.meta().attrs().get_debug_impl();
let partial_eq_fn = reflect_enum
.meta()
.traits()
.attrs()
.get_partial_eq_impl(bevy_reflect_path)
.unwrap_or_else(|| {
quote! {
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/structs.rs
Expand Up @@ -32,11 +32,11 @@ pub(crate) fn impl_struct(reflect_struct: &ReflectStruct) -> proc_macro2::TokenS

let hash_fn = reflect_struct
.meta()
.traits()
.attrs()
.get_hash_impl(bevy_reflect_path);
let debug_fn = reflect_struct.meta().traits().get_debug_impl();
let debug_fn = reflect_struct.meta().attrs().get_debug_impl();
let partial_eq_fn = reflect_struct.meta()
.traits()
.attrs()
.get_partial_eq_impl(bevy_reflect_path)
.unwrap_or_else(|| {
quote! {
Expand Down
Expand Up @@ -24,12 +24,12 @@ pub(crate) fn impl_tuple_struct(reflect_struct: &ReflectStruct) -> proc_macro2::

let hash_fn = reflect_struct
.meta()
.traits()
.attrs()
.get_hash_impl(bevy_reflect_path);
let debug_fn = reflect_struct.meta().traits().get_debug_impl();
let debug_fn = reflect_struct.meta().attrs().get_debug_impl();
let partial_eq_fn = reflect_struct
.meta()
.traits()
.attrs()
.get_partial_eq_impl(bevy_reflect_path)
.unwrap_or_else(|| {
quote! {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_reflect/bevy_reflect_derive/src/impls/typed.rs
Expand Up @@ -51,7 +51,7 @@ pub(crate) enum TypedProperty {
pub(crate) fn impl_type_path(meta: &ReflectMeta) -> proc_macro2::TokenStream {
let where_clause_options = WhereClauseOptions::new(meta);

if !meta.traits().type_path_attrs().should_auto_derive() {
if !meta.attrs().type_path_attrs().should_auto_derive() {
return proc_macro2::TokenStream::new();
}

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/bevy_reflect_derive/src/impls/values.rs
Expand Up @@ -9,9 +9,9 @@ pub(crate) fn impl_value(meta: &ReflectMeta) -> proc_macro2::TokenStream {
let bevy_reflect_path = meta.bevy_reflect_path();
let type_path = meta.type_path();

let hash_fn = meta.traits().get_hash_impl(bevy_reflect_path);
let partial_eq_fn = meta.traits().get_partial_eq_impl(bevy_reflect_path);
let debug_fn = meta.traits().get_debug_impl();
let hash_fn = meta.attrs().get_hash_impl(bevy_reflect_path);
let partial_eq_fn = meta.attrs().get_partial_eq_impl(bevy_reflect_path);
let debug_fn = meta.attrs().get_debug_impl();

#[cfg(feature = "documentation")]
let with_docs = {
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/lib.rs
Expand Up @@ -30,7 +30,7 @@ mod type_path;
mod utility;

use crate::derive_data::{ReflectDerive, ReflectMeta, ReflectStruct};
use container_attributes::ReflectTraits;
use container_attributes::ContainerAttributes;
use derive_data::{ReflectImplSource, ReflectProvenance, ReflectTraitToImpl, ReflectTypePath};
use proc_macro::TokenStream;
use quote::quote;
Expand Down Expand Up @@ -656,7 +656,7 @@ pub fn impl_type_path(input: TokenStream) -> TokenStream {
NamedTypePathDef::Primitive(ref ident) => ReflectTypePath::Primitive(ident),
};

let meta = ReflectMeta::new(type_path, ReflectTraits::default());
let meta = ReflectMeta::new(type_path, ContainerAttributes::default());

let type_path_impl = impls::impl_type_path(&meta);

Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_reflect/bevy_reflect_derive/src/reflect_value.rs
@@ -1,4 +1,4 @@
use crate::container_attributes::ReflectTraits;
use crate::container_attributes::ContainerAttributes;
use crate::derive_data::ReflectTraitToImpl;
use crate::type_path::CustomPathDef;
use syn::parse::ParseStream;
Expand Down Expand Up @@ -29,7 +29,7 @@ pub(crate) struct ReflectValueDef {
pub attrs: Vec<Attribute>,
pub type_path: Path,
pub generics: Generics,
pub traits: Option<ReflectTraits>,
pub traits: Option<ContainerAttributes>,
pub custom_path: Option<CustomPathDef>,
}

Expand All @@ -55,7 +55,7 @@ impl ReflectValueDef {
if input.peek(Paren) {
let content;
parenthesized!(content in input);
traits = Some(ReflectTraits::parse_terminated(&content, trait_)?);
traits = Some(ContainerAttributes::parse_terminated(&content, trait_)?);
}
Ok(ReflectValueDef {
attrs,
Expand Down
Expand Up @@ -14,7 +14,7 @@ pub(crate) fn impl_get_type_registration(
) -> proc_macro2::TokenStream {
let type_path = meta.type_path();
let bevy_reflect_path = meta.bevy_reflect_path();
let registration_data = meta.traits().idents();
let registration_data = meta.attrs().idents();
let (impl_generics, ty_generics, where_clause) = type_path.generics().split_for_impl();
let where_reflect_clause = where_clause_options.extend_where_clause(where_clause);

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_reflect/bevy_reflect_derive/src/utility.rs
Expand Up @@ -184,7 +184,7 @@ impl<'a, 'b> WhereClauseOptions<'a, 'b> {
predicates.extend(field_predicates);
}

if let Some(custom_where) = self.meta.traits().custom_where() {
if let Some(custom_where) = self.meta.attrs().custom_where() {
predicates.push(custom_where.predicates.to_token_stream());
}

Expand All @@ -209,7 +209,7 @@ impl<'a, 'b> WhereClauseOptions<'a, 'b> {

/// Returns an iterator over the where clause predicates for the active fields.
fn active_field_predicates(&self) -> Option<impl Iterator<Item = TokenStream> + '_> {
if self.meta.traits().no_field_bounds() {
if self.meta.attrs().no_field_bounds() {
None
} else {
let bevy_reflect_path = self.meta.bevy_reflect_path();
Expand Down

0 comments on commit 1472fc9

Please sign in to comment.