From abdc4c64db08d111df14eb1a687a23d9d232b62d Mon Sep 17 00:00:00 2001 From: radiish Date: Sat, 21 Jan 2023 15:28:32 +0000 Subject: [PATCH 1/9] `Debug` -> `debug` remove `Default` special casing --- crates/bevy_asset/src/handle.rs | 2 +- crates/bevy_asset/src/path.rs | 6 +- crates/bevy_ecs/src/reflect.rs | 2 +- .../bevy_hierarchy/src/components/parent.rs | 2 +- crates/bevy_input/src/gamepad.rs | 30 ++-- crates/bevy_input/src/keyboard.rs | 6 +- crates/bevy_input/src/lib.rs | 2 +- crates/bevy_input/src/mouse.rs | 10 +- crates/bevy_input/src/touch.rs | 6 +- crates/bevy_pbr/src/alpha.rs | 2 +- crates/bevy_pbr/src/pbr_material.rs | 2 +- .../src/container_attributes.rs | 160 +++++++----------- .../bevy_reflect_derive/src/derive_data.rs | 32 +++- .../bevy_reflect_derive/src/from_reflect.rs | 56 +++--- .../bevy_reflect_derive/src/lib.rs | 11 +- .../bevy_reflect_derive/src/registration.rs | 2 +- .../src/trait_reflection.rs | 4 +- .../bevy_reflect_derive/src/utility.rs | 12 +- crates/bevy_reflect/src/impls/glam.rs | 64 +++---- crates/bevy_reflect/src/impls/rect.rs | 2 +- crates/bevy_reflect/src/impls/std.rs | 104 ++++++------ crates/bevy_reflect/src/lib.rs | 17 +- crates/bevy_reflect/src/serde/mod.rs | 4 +- crates/bevy_render/src/color/mod.rs | 2 +- .../src/view/visibility/render_layers.rs | 2 +- .../bevy_sprite/src/mesh2d/color_material.rs | 2 +- crates/bevy_sprite/src/texture_atlas.rs | 2 +- .../src/components/global_transform.rs | 2 +- .../src/components/transform.rs | 2 +- crates/bevy_ui/src/focus.rs | 6 +- crates/bevy_ui/src/geometry.rs | 4 +- crates/bevy_ui/src/ui_node.rs | 24 +-- crates/bevy_window/src/cursor.rs | 2 +- crates/bevy_window/src/event.rs | 28 +-- crates/bevy_window/src/window.rs | 20 +-- examples/reflection/reflection_types.rs | 4 +- 36 files changed, 309 insertions(+), 329 deletions(-) diff --git a/crates/bevy_asset/src/handle.rs b/crates/bevy_asset/src/handle.rs index dfa4bd2d35892..11678350aeefa 100644 --- a/crates/bevy_asset/src/handle.rs +++ b/crates/bevy_asset/src/handle.rs @@ -32,7 +32,7 @@ use serde::{Deserialize, Serialize}; Reflect, FromReflect, )] -#[reflect_value(Serialize, Deserialize, PartialEq, Hash)] +#[reflect_value(partial_eq, hash, Serialize, Deserialize)] pub enum HandleId { /// A handle id of a loaded asset. Id(Uuid, u64), diff --git a/crates/bevy_asset/src/path.rs b/crates/bevy_asset/src/path.rs index e9150c63a8834..afc0ba6fc6c86 100644 --- a/crates/bevy_asset/src/path.rs +++ b/crates/bevy_asset/src/path.rs @@ -68,21 +68,21 @@ impl<'a> AssetPath<'a> { #[derive( Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Reflect, )] -#[reflect_value(PartialEq, Hash, Serialize, Deserialize)] +#[reflect_value(partial_eq, hash, Serialize, Deserialize)] pub struct AssetPathId(SourcePathId, LabelId); /// An unique identifier to the source path of an asset. #[derive( Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Reflect, )] -#[reflect_value(PartialEq, Hash, Serialize, Deserialize)] +#[reflect_value(partial_eq, hash, Serialize, Deserialize)] pub struct SourcePathId(u64); /// An unique identifier to a sub-asset label. #[derive( Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize, Reflect, )] -#[reflect_value(PartialEq, Hash, Serialize, Deserialize)] +#[reflect_value(partial_eq, hash, Serialize, Deserialize)] pub struct LabelId(u64); impl<'a> From<&'a Path> for SourcePathId { diff --git a/crates/bevy_ecs/src/reflect.rs b/crates/bevy_ecs/src/reflect.rs index 88e25dfaf86b5..93f68943d7b4a 100644 --- a/crates/bevy_ecs/src/reflect.rs +++ b/crates/bevy_ecs/src/reflect.rs @@ -401,7 +401,7 @@ impl FromType for ReflectResource { } } -impl_reflect_value!(Entity(Hash, PartialEq, Serialize, Deserialize)); +impl_reflect_value!(Entity(hash, partial_eq, Serialize, Deserialize)); impl_from_reflect_value!(Entity); #[derive(Clone)] diff --git a/crates/bevy_hierarchy/src/components/parent.rs b/crates/bevy_hierarchy/src/components/parent.rs index 3e573be5a6952..293e8d087caf3 100644 --- a/crates/bevy_hierarchy/src/components/parent.rs +++ b/crates/bevy_hierarchy/src/components/parent.rs @@ -15,7 +15,7 @@ use std::ops::Deref; /// [`HierarchyQueryExt`]: crate::query_extension::HierarchyQueryExt /// [`Query`]: bevy_ecs::system::Query #[derive(Component, Debug, Eq, PartialEq, Reflect)] -#[reflect(Component, MapEntities, PartialEq)] +#[reflect(partial_eq, Component, MapEntities)] pub struct Parent(pub(crate) Entity); impl Parent { diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index cc8101d6810a6..f44a91cc5a8ed 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -72,7 +72,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// /// The `ID` of a gamepad is fixed until the gamepad disconnects or the app is restarted. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -92,7 +92,7 @@ impl Gamepad { /// Metadata associated with a `Gamepad`. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -153,7 +153,7 @@ impl Gamepads { /// which in turn is used to create the [`Input`] or /// [`Axis`] `bevy` resources. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -218,7 +218,7 @@ pub enum GamepadButtonType { /// /// The gamepad button resources are updated inside of the [`gamepad_button_event_system`]. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -260,7 +260,7 @@ impl GamepadButton { /// [`GamepadAxisChangedEvent`]. It is also used in the [`GamepadAxis`] /// which in turn is used to create the [`Axis`] `bevy` resource. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -296,7 +296,7 @@ pub enum GamepadAxisType { /// /// The gamepad axes resources are updated inside of the [`gamepad_axis_event_system`]. #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -341,7 +341,7 @@ impl GamepadAxis { /// should register as a [`GamepadEvent`]. Events that don't meet the change thresholds defined in [`GamepadSettings`] /// will not register. To modify these settings, mutate the corresponding resource. #[derive(Resource, Default, Debug, Reflect, FromReflect)] -#[reflect(Debug, Default)] +#[reflect(debug, Default)] pub struct GamepadSettings { /// The default button settings. pub default_button_settings: ButtonSettings, @@ -424,7 +424,7 @@ impl GamepadSettings { /// /// Allowed values: `0.0 <= ``release_threshold`` <= ``press_threshold`` <= 1.0` #[derive(Debug, Clone, Reflect, FromReflect)] -#[reflect(Debug, Default)] +#[reflect(debug, Default)] pub struct ButtonSettings { press_threshold: f32, release_threshold: f32, @@ -584,7 +584,7 @@ impl ButtonSettings { /// /// The valid range is `[-1.0, 1.0]`. #[derive(Debug, Clone, Reflect, FromReflect, PartialEq)] -#[reflect(Debug, Default)] +#[reflect(debug, Default)] pub struct AxisSettings { /// Values that are higher than `livezone_upperbound` will be rounded up to -1.0. livezone_upperbound: f32, @@ -915,7 +915,7 @@ impl AxisSettings { /// /// The current value of a button is received through the [`GamepadButtonChangedEvent`]. #[derive(Debug, Clone, Reflect, FromReflect)] -#[reflect(Debug, Default)] +#[reflect(debug, Default)] pub struct ButtonAxisSettings { /// The high value at which to apply rounding. pub high: f32, @@ -1024,7 +1024,7 @@ pub fn gamepad_connection_system( } #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -1038,7 +1038,7 @@ pub enum GamepadConnection { /// A Gamepad connection event. Created when a connection to a gamepad /// is established and when a gamepad is disconnected. #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -1069,7 +1069,7 @@ impl GamepadConnectionEvent { } #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -1094,7 +1094,7 @@ impl GamepadAxisChangedEvent { /// Gamepad event for when the "value" (amount of pressure) on the button /// changes by an amount larger than the threshold defined in [`GamepadSettings`]. #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -1157,7 +1157,7 @@ pub fn gamepad_button_event_system( /// [`GamepadButtonChangedEvent`] and [`GamepadAxisChangedEvent`] when /// the in-frame relative ordering of events is important. #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), diff --git a/crates/bevy_input/src/keyboard.rs b/crates/bevy_input/src/keyboard.rs index 83b35fe47e0a8..1e792be19ddcd 100644 --- a/crates/bevy_input/src/keyboard.rs +++ b/crates/bevy_input/src/keyboard.rs @@ -15,7 +15,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// The event is consumed inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system) /// to update the [`Input`](crate::Input) resource. #[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -72,7 +72,7 @@ pub fn keyboard_input_system( /// /// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system). #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -439,7 +439,7 @@ pub enum KeyCode { /// /// The resource is updated inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system). #[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), diff --git a/crates/bevy_input/src/lib.rs b/crates/bevy_input/src/lib.rs index fccc2027f56b4..ecf81154766a9 100644 --- a/crates/bevy_input/src/lib.rs +++ b/crates/bevy_input/src/lib.rs @@ -132,7 +132,7 @@ impl Plugin for InputPlugin { /// The current "press" state of an element #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), diff --git a/crates/bevy_input/src/mouse.rs b/crates/bevy_input/src/mouse.rs index 9fbac1ce937ff..4e8f3d9735233 100644 --- a/crates/bevy_input/src/mouse.rs +++ b/crates/bevy_input/src/mouse.rs @@ -15,7 +15,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// The event is read inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system) /// to update the [`Input`](crate::Input) resource. #[derive(Debug, Clone, Copy, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -39,7 +39,7 @@ pub struct MouseButtonInput { /// /// The resource is updated inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system). #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -66,7 +66,7 @@ pub enum MouseButton { /// /// [`DeviceEvent::MouseMotion`]: https://docs.rs/winit/latest/winit/event/enum.DeviceEvent.html#variant.MouseMotion #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -84,7 +84,7 @@ pub struct MouseMotion { /// The value of the event can either be interpreted as the amount of lines or the amount of pixels /// to scroll. #[derive(Debug, Clone, Copy, Eq, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -107,7 +107,7 @@ pub enum MouseScrollUnit { /// /// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), diff --git a/crates/bevy_input/src/touch.rs b/crates/bevy_input/src/touch.rs index 3f00d994fd3f0..c07f234657dbe 100644 --- a/crates/bevy_input/src/touch.rs +++ b/crates/bevy_input/src/touch.rs @@ -31,7 +31,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// This event is the translated version of the `WindowEvent::Touch` from the `winit` crate. /// It is available to the end user and can be used for game logic. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -53,7 +53,7 @@ pub struct TouchInput { /// A force description of a [`Touch`](crate::touch::Touch) input. #[derive(Debug, Clone, Copy, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -99,7 +99,7 @@ pub enum ForceTouch { /// or that a finger has moved. There is also a cancelled phase that indicates that /// the system cancelled the tracking of the finger. #[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, Reflect, FromReflect)] -#[reflect(Debug, Hash, PartialEq)] +#[reflect(debug, hash, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), diff --git a/crates/bevy_pbr/src/alpha.rs b/crates/bevy_pbr/src/alpha.rs index b42a78bd8ad4d..dab549ba18e00 100644 --- a/crates/bevy_pbr/src/alpha.rs +++ b/crates/bevy_pbr/src/alpha.rs @@ -5,7 +5,7 @@ use bevy_reflect::{FromReflect, Reflect}; // TODO: add discussion about performance. /// Sets how a material's base color alpha channel is used for transparency. #[derive(Component, Debug, Default, Reflect, Copy, Clone, PartialEq, FromReflect)] -#[reflect(Component, Default, Debug)] +#[reflect(debug, Component, Default)] pub enum AlphaMode { /// Base color alpha values are overridden to be fully opaque (1.0). #[default] diff --git a/crates/bevy_pbr/src/pbr_material.rs b/crates/bevy_pbr/src/pbr_material.rs index 629fee943c4d0..cc067149dbf8e 100644 --- a/crates/bevy_pbr/src/pbr_material.rs +++ b/crates/bevy_pbr/src/pbr_material.rs @@ -19,7 +19,7 @@ use bevy_render::{ #[uuid = "7494888b-c082-457b-aacf-517228cc0c22"] #[bind_group_data(StandardMaterialKey)] #[uniform(0, StandardMaterialUniform)] -#[reflect(Default, Debug)] +#[reflect(debug, Default)] pub struct StandardMaterial { /// The color of the surface of the material before lighting. /// diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs index 94749f0e8a4e7..cbb0bcb00830a 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs @@ -3,30 +3,23 @@ //! A container attribute is an attribute which applies to an entire struct or enum //! as opposed to a particular field or variant. An example of such an attribute is //! the derive helper attribute for `Reflect`, which looks like: -//! `#[reflect(PartialEq, Default, ...)]` and `#[reflect_value(PartialEq, Default, ...)]`. +//! `#[reflect(partial_eq, Default, ...)]` and `#[reflect_value(PartialEq, Default, ...)]`. use crate::fq_std::{FQAny, FQDefault, FQOption}; use crate::utility; -use proc_macro2::{Ident, Span}; +use proc_macro2::Span; use quote::quote_spanned; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::token::Comma; -use syn::{Meta, NestedMeta, Path}; +use syn::{Meta, NestedMeta, Path, Lit, parse_str}; -// The "special" trait idents that are used internally for reflection. -// Received via attributes like `#[reflect(PartialEq, Hash, ...)]` -const DEBUG_ATTR: &str = "Debug"; -const PARTIAL_EQ_ATTR: &str = "PartialEq"; -const HASH_ATTR: &str = "Hash"; - -// The traits listed below are not considered "special" (i.e. they use the `ReflectMyTrait` syntax) -// but useful to know exist nonetheless -pub(crate) const REFLECT_DEFAULT: &str = "ReflectDefault"; - -// The error message to show when a trait/type is specified multiple times -const CONFLICTING_TYPE_DATA_MESSAGE: &str = "conflicting type data registration"; +// The "special" idents that are used internally for reflection. +// Received via attributes like `#[reflect(partial_eq, hash, ...)]` +const DEBUG_ATTR: &str = "debug"; +const PARTIAL_EQ_ATTR: &str = "partial_eq"; +const HASH_ATTR: &str = "hash"; /// A marker for trait implementations registered via the `Reflect` derive macro. #[derive(Clone, Default)] @@ -52,7 +45,7 @@ impl TraitImpl { match (self, other) { (TraitImpl::NotImplemented, value) | (value, TraitImpl::NotImplemented) => Ok(value), (_, TraitImpl::Implemented(span) | TraitImpl::Custom(_, span)) => { - Err(syn::Error::new(span, CONFLICTING_TYPE_DATA_MESSAGE)) + Err(syn::Error::new(span, "conflicting type data registration")) } } } @@ -66,9 +59,9 @@ impl TraitImpl { /// `Reflect` derive macro using the helper attribute: `#[reflect(...)]`. /// /// The list of special traits are as follows: -/// * `Debug` -/// * `Hash` -/// * `PartialEq` +/// * `debug` +/// * `hash` +/// * `partial_eq` /// /// When registering a trait, there are a few things to keep in mind: /// * Traits must have a valid `Reflect{}` struct in scope. For example, `Default` @@ -92,28 +85,28 @@ impl TraitImpl { /// struct Foo; /// ``` /// -/// Registering the `Hash` implementation: +/// Registering `hash` using the `Hash` trait: /// /// ```ignore -/// // `Hash` is a "special trait" and does not need (nor have) a ReflectHash struct +/// // `hash` is a "special trait" and does not need (nor have) a ReflectHash struct /// /// #[derive(Reflect, Hash)] -/// #[reflect(Hash)] +/// #[reflect(hash)] /// struct Foo; /// ``` /// -/// Registering the `Hash` implementation using a custom function: +/// Registering `hash` using a custom function: /// /// ```ignore -/// // This function acts as our `Hash` implementation and +/// // This function acts is out hash function and /// // corresponds to the `Reflect::reflect_hash` method. /// fn get_hash(foo: &Foo) -> Option { /// Some(123) /// } /// /// #[derive(Reflect)] -/// // Register the custom `Hash` function -/// #[reflect(Hash(get_hash))] +/// // Register the custom hash function +/// #[reflect(hash = "get_hash")] /// struct Foo; /// ``` /// @@ -124,7 +117,7 @@ pub(crate) struct ReflectTraits { debug: TraitImpl, hash: TraitImpl, partial_eq: TraitImpl, - idents: Vec, + idents: Vec, } impl ReflectTraits { @@ -135,67 +128,51 @@ impl ReflectTraits { let mut traits = ReflectTraits::default(); for nested_meta in nested_metas.iter() { match nested_meta { - // Handles `#[reflect( Hash, Default, ... )]` + // Handles `#[reflect(hash = "custom_hash_fn")]` + NestedMeta::Meta(Meta::NameValue(name_value)) => { + if let Lit::Str(lit) = &name_value.lit { + // This should be the path of the custom function + let path: Path = parse_str(&lit.value())?; + // Track the span where the trait is implemented for future errors + let span = lit.span(); + let trait_func_ident = TraitImpl::Custom(path, span); + match name_value.path.get_ident().map(ToString::to_string).as_deref() { + Some(DEBUG_ATTR) => { + traits.debug = traits.debug.merge(trait_func_ident)?; + } + Some(PARTIAL_EQ_ATTR) => { + traits.partial_eq = traits.partial_eq.merge(trait_func_ident)?; + } + Some(HASH_ATTR) => { + traits.hash = traits.hash.merge(trait_func_ident)?; + } + _ => { + return Err(syn::Error::new(span, "custom path literals can only be used for \"special\" idents.")) + } + } + } + }, + // Handles `#[reflect( hash, Default, ... )]` NestedMeta::Meta(Meta::Path(path)) => { - // Get the first ident in the path (hopefully the path only contains one and not `std::hash::Hash`) - let Some(segment) = path.segments.iter().next() else { - continue; - }; - let ident = &segment.ident; - let ident_name = ident.to_string(); - // Track the span where the trait is implemented for future errors - let span = ident.span(); + let span = path.span(); - match ident_name.as_str() { - DEBUG_ATTR => { + match path.get_ident().map(ToString::to_string).as_deref() { + Some(DEBUG_ATTR) => { traits.debug = traits.debug.merge(TraitImpl::Implemented(span))?; } - PARTIAL_EQ_ATTR => { + Some(PARTIAL_EQ_ATTR) => { traits.partial_eq = traits.partial_eq.merge(TraitImpl::Implemented(span))?; } - HASH_ATTR => { + Some(HASH_ATTR) => { traits.hash = traits.hash.merge(TraitImpl::Implemented(span))?; } - // We only track reflected idents for traits not considered special _ => { // Create the reflect ident // We set the span to the old ident so any compile errors point to that ident instead - let mut reflect_ident = utility::get_reflect_ident(&ident_name); - reflect_ident.set_span(span); - - add_unique_ident(&mut traits.idents, reflect_ident)?; - } - } - } - // Handles `#[reflect( Hash(custom_hash_fn) )]` - NestedMeta::Meta(Meta::List(list)) => { - // Get the first ident in the path (hopefully the path only contains one and not `std::hash::Hash`) - let Some(segment) = list.path.segments.iter().next() else { - continue; - }; - - let ident = segment.ident.to_string(); - - // Track the span where the trait is implemented for future errors - let span = ident.span(); - - let list_meta = list.nested.iter().next(); - if let Some(NestedMeta::Meta(Meta::Path(path))) = list_meta { - // This should be the path of the custom function - let trait_func_ident = TraitImpl::Custom(path.clone(), span); - match ident.as_str() { - DEBUG_ATTR => { - traits.debug = traits.debug.merge(trait_func_ident)?; - } - PARTIAL_EQ_ATTR => { - traits.partial_eq = traits.partial_eq.merge(trait_func_ident)?; - } - HASH_ATTR => { - traits.hash = traits.hash.merge(trait_func_ident)?; - } - _ => {} + let reflect_ident = utility::into_reflected_path(path.clone()); + traits.idents.push(reflect_ident); } } } @@ -206,20 +183,14 @@ impl ReflectTraits { Ok(traits) } - /// Returns true if the given reflected trait name (i.e. `ReflectDefault` for `Default`) - /// is registered for this type. - pub fn contains(&self, name: &str) -> bool { - self.idents.iter().any(|ident| ident == name) - } - /// The list of reflected traits by their reflected ident (i.e. `ReflectDefault` for `Default`). - pub fn idents(&self) -> &[Ident] { + pub fn paths(&self) -> &[Path] { &self.idents } /// Returns the implementation of `Reflect::reflect_hash` as a `TokenStream`. /// - /// If `Hash` was not registered, returns `None`. + /// If `hash` was not registered, returns `None`. pub fn get_hash_impl(&self, bevy_reflect_path: &Path) -> Option { match &self.hash { &TraitImpl::Implemented(span) => Some(quote_spanned! {span=> @@ -242,7 +213,7 @@ impl ReflectTraits { /// Returns the implementation of `Reflect::reflect_partial_eq` as a `TokenStream`. /// - /// If `PartialEq` was not registered, returns `None`. + /// If `partial_eq` was not registered, returns `None`. pub fn get_partial_eq_impl( &self, bevy_reflect_path: &Path, @@ -269,7 +240,7 @@ impl ReflectTraits { /// Returns the implementation of `Reflect::debug` as a `TokenStream`. /// - /// If `Debug` was not registered, returns `None`. + /// If `debug` was not registered, returns `None`. pub fn get_debug_impl(&self) -> Option { match &self.debug { &TraitImpl::Implemented(span) => Some(quote_spanned! {span=> @@ -296,9 +267,7 @@ impl ReflectTraits { partial_eq: self.partial_eq.merge(other.partial_eq)?, idents: { let mut idents = self.idents; - for ident in other.idents { - add_unique_ident(&mut idents, ident)?; - } + idents.extend(other.idents); idents }, }) @@ -310,17 +279,4 @@ impl Parse for ReflectTraits { let result = Punctuated::::parse_terminated(input)?; ReflectTraits::from_nested_metas(&result) } -} - -/// Adds an identifier to a vector of identifiers if it is not already present. -/// -/// Returns an error if the identifier already exists in the list. -fn add_unique_ident(idents: &mut Vec, ident: Ident) -> Result<(), syn::Error> { - let ident_name = ident.to_string(); - if idents.iter().any(|i| i == ident_name.as_str()) { - return Err(syn::Error::new(ident.span(), CONFLICTING_TYPE_DATA_MESSAGE)); - } - - idents.push(ident); - Ok(()) -} +} \ No newline at end of file diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs b/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs index 92b2035e1939d..99f65dbac2310 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs @@ -17,6 +17,14 @@ pub(crate) enum ReflectDerive<'a> { Value(ReflectMeta<'a>), } +/// The context of the implementation. +#[derive(Clone, Copy)] +pub(crate) enum ReflectImplSource { + Derive, + ImplStruct, + ImplValue, +} + /// Metadata present on all reflected types, including name, generics, and attributes. /// /// # Example @@ -25,7 +33,7 @@ pub(crate) enum ReflectDerive<'a> { /// #[derive(Reflect)] /// // traits /// // |----------------------------------------| -/// #[reflect(PartialEq, Serialize, Deserialize, Default)] +/// #[reflect(partial_eq, Serialize, Deserialize, Default)] /// // type_name generics /// // |-------------------||----------| /// struct ThingThatImReflecting {/* ... */} @@ -39,6 +47,8 @@ pub(crate) struct ReflectMeta<'a> { generics: &'a Generics, /// A cached instance of the path to the `bevy_reflect` crate. bevy_reflect_path: Path, + /// In what context the type is being reflected. + impl_source: ReflectImplSource, /// The documentation for this type, if any #[cfg(feature = "documentation")] docs: crate::documentation::Documentation, @@ -50,7 +60,7 @@ pub(crate) struct ReflectMeta<'a> { /// /// ```ignore /// #[derive(Reflect)] -/// #[reflect(PartialEq, Serialize, Deserialize, Default)] +/// #[reflect(partial_eq, Serialize, Deserialize, Default)] /// struct ThingThatImReflecting { /// x: T1, // | /// y: T2, // |- fields @@ -69,7 +79,7 @@ pub(crate) struct ReflectStruct<'a> { /// /// ```ignore /// #[derive(Reflect)] -/// #[reflect(PartialEq, Serialize, Deserialize, Default)] +/// #[reflect(partial_eq, Serialize, Deserialize, Default)] /// enum ThingThatImReflecting { /// A(T1), // | /// B, // |- variants @@ -127,7 +137,7 @@ enum ReflectMode { } impl<'a> ReflectDerive<'a> { - pub fn from_input(input: &'a DeriveInput) -> Result { + pub fn from_input(input: &'a DeriveInput, impl_source: ReflectImplSource) -> Result { let mut traits = ReflectTraits::default(); // Should indicate whether `#[reflect_value]` was used let mut reflect_mode = None; @@ -181,7 +191,7 @@ impl<'a> ReflectDerive<'a> { } } - let meta = ReflectMeta::new(&input.ident, &input.generics, traits); + let meta = ReflectMeta::new(&input.ident, &input.generics, traits, impl_source); #[cfg(feature = "documentation")] let meta = meta.with_docs(doc); @@ -278,11 +288,12 @@ impl<'a> ReflectDerive<'a> { } impl<'a> ReflectMeta<'a> { - pub fn new(type_name: &'a Ident, generics: &'a Generics, traits: ReflectTraits) -> Self { + pub fn new(type_name: &'a Ident, generics: &'a Generics, traits: ReflectTraits, impl_source: ReflectImplSource) -> Self { Self { traits, type_name, generics, + impl_source, bevy_reflect_path: utility::get_bevy_reflect_path(), #[cfg(feature = "documentation")] docs: Default::default(), @@ -310,6 +321,11 @@ impl<'a> ReflectMeta<'a> { self.generics } + /// In what context the type is being reflected. + pub fn impl_source(&self) -> ReflectImplSource { + self.impl_source + } + /// The cached `bevy_reflect` path. pub fn bevy_reflect_path(&self) -> &Path { &self.bevy_reflect_path @@ -320,7 +336,7 @@ impl<'a> ReflectMeta<'a> { crate::registration::impl_get_type_registration( self.type_name, &self.bevy_reflect_path, - self.traits.idents(), + self.traits.paths(), self.generics, None, ) @@ -356,7 +372,7 @@ impl<'a> ReflectStruct<'a> { crate::registration::impl_get_type_registration( self.meta.type_name(), reflect_path, - self.meta.traits().idents(), + self.meta.traits().paths(), self.meta.generics(), Some(&self.serialization_denylist), ) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs b/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs index aab2545b54989..38ac9a7ccf699 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs @@ -1,5 +1,4 @@ -use crate::container_attributes::REFLECT_DEFAULT; -use crate::derive_data::ReflectEnum; +use crate::derive_data::{ReflectEnum, ReflectImplSource}; use crate::enum_utility::{get_variant_constructors, EnumVariantConstructors}; use crate::field_attributes::DefaultBehavior; use crate::fq_std::{FQAny, FQClone, FQDefault, FQOption}; @@ -75,8 +74,6 @@ impl MemberValuePair { } fn impl_struct_internal(reflect_struct: &ReflectStruct, is_tuple: bool) -> TokenStream { - let fqoption = FQOption.into_token_stream(); - let struct_name = reflect_struct.meta().type_name(); let generics = reflect_struct.meta().generics(); let bevy_reflect_path = reflect_struct.meta().bevy_reflect_path(); @@ -92,29 +89,36 @@ fn impl_struct_internal(reflect_struct: &ReflectStruct, is_tuple: bool) -> Token let MemberValuePair(active_members, active_values) = get_active_fields(reflect_struct, &ref_struct, &ref_struct_type, is_tuple); - let constructor = if reflect_struct.meta().traits().contains(REFLECT_DEFAULT) { - quote!( - let mut __this: Self = #FQDefault::default(); - #( - if let #fqoption::Some(__field) = #active_values() { - // Iff field exists -> use its value - __this.#active_members = __field; - } - )* - #FQOption::Some(__this) - ) - } else { - let MemberValuePair(ignored_members, ignored_values) = - get_ignored_fields(reflect_struct, is_tuple); - - quote!( - #FQOption::Some( - Self { - #(#active_members: #active_values()?,)* - #(#ignored_members: #ignored_values,)* - } + let MemberValuePair(ignored_members, ignored_values) = + get_ignored_fields(reflect_struct, is_tuple); + + let constructor = match reflect_struct.meta().impl_source() { + ReflectImplSource::ImplStruct => { + quote!( + // This is necessary for `impl_reflect_struct` on types like `Vec4` + // because on some targets field access is implemented using `Deref(Mut)`. + let mut __this: Self = #FQDefault::default(); + #( + // Early-return if an active field is not present. + __this.#active_members = #active_values()?; + )* + #( + __this.#ignored_members = #ignored_values; + )* + #FQOption::Some(__this) + ) + }, + _ => { + + quote!( + #FQOption::Some( + Self { + #(#active_members: #active_values()?,)* + #(#ignored_members: #ignored_values,)* + } + ) ) - ) + } }; let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs index eb486d86baa31..65808a075533c 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs @@ -30,6 +30,7 @@ mod type_uuid; mod utility; use crate::derive_data::{ReflectDerive, ReflectMeta, ReflectStruct}; +use derive_data::ReflectImplSource; use proc_macro::TokenStream; use quote::quote; use reflect_value::ReflectValueDef; @@ -43,7 +44,7 @@ pub(crate) static REFLECT_VALUE_ATTRIBUTE_NAME: &str = "reflect_value"; pub fn derive_reflect(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); - let derive_data = match ReflectDerive::from_input(&ast) { + let derive_data = match ReflectDerive::from_input(&ast, ReflectImplSource::Derive) { Ok(data) => data, Err(err) => return err.into_compile_error().into(), }; @@ -69,7 +70,7 @@ pub fn derive_reflect(input: TokenStream) -> TokenStream { pub fn derive_from_reflect(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); - let derive_data = match ReflectDerive::from_input(&ast) { + let derive_data = match ReflectDerive::from_input(&ast, ReflectImplSource::Derive) { Ok(data) => data, Err(err) => return err.into_compile_error().into(), }; @@ -102,6 +103,7 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream { &def.type_name, &def.generics, def.traits.unwrap_or_default(), + ReflectImplSource::ImplValue ); #[cfg(feature = "documentation")] @@ -130,7 +132,7 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream { /// use bevy::prelude::Vec3; /// /// impl_reflect_struct!( -/// #[reflect(PartialEq, Serialize, Deserialize, Default)] +/// #[reflect(partial_eq, Serialize, Deserialize, Default)] /// struct Vec3 { /// x: f32, /// y: f32, @@ -141,7 +143,7 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream { #[proc_macro] pub fn impl_reflect_struct(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); - let derive_data = match ReflectDerive::from_input(&ast) { + let derive_data = match ReflectDerive::from_input(&ast, ReflectImplSource::ImplStruct) { Ok(data) => data, Err(err) => return err.into_compile_error().into(), }; @@ -183,5 +185,6 @@ pub fn impl_from_reflect_value(input: TokenStream) -> TokenStream { &def.type_name, &def.generics, def.traits.unwrap_or_default(), + ReflectImplSource::ImplValue, )) } diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs b/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs index 456e4152ca85f..48eca1ff8b2d5 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/registration.rs @@ -9,7 +9,7 @@ use syn::{Generics, Path}; pub(crate) fn impl_get_type_registration( type_name: &Ident, bevy_reflect_path: &Path, - registration_data: &[Ident], + registration_data: &[Path], generics: &Generics, serialization_denylist: Option<&BitSet>, ) -> proc_macro2::TokenStream { diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs b/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs index 14bd9d6d40674..4673fd4b80ebd 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs @@ -2,7 +2,7 @@ use crate::fq_std::{FQBox, FQClone, FQOption, FQResult}; use bevy_macro_utils::BevyManifest; use proc_macro::TokenStream; use quote::quote; -use syn::{parse::Parse, parse_macro_input, Attribute, ItemTrait, Token}; +use syn::{parse::Parse, parse_macro_input, Attribute, ItemTrait, Token, Path}; pub(crate) struct TraitInfo { item_trait: ItemTrait, @@ -31,7 +31,7 @@ pub(crate) fn reflect_trait(_args: &TokenStream, input: TokenStream) -> TokenStr let item_trait = &trait_info.item_trait; let trait_ident = &item_trait.ident; let trait_vis = &item_trait.vis; - let reflect_trait_ident = crate::utility::get_reflect_ident(&item_trait.ident.to_string()); + let reflect_trait_ident = crate::utility::into_reflected_path(Path::from(trait_ident.clone())); let bevy_reflect_path = BevyManifest::default().get_path("bevy_reflect"); let struct_doc = format!( diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs index d9e44754c9f54..9e19a0359bc1b 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs @@ -3,8 +3,8 @@ use crate::field_attributes::ReflectIgnoreBehavior; use bevy_macro_utils::BevyManifest; use bit_set::BitSet; -use proc_macro2::{Ident, Span}; -use syn::{Member, Path}; +use proc_macro2::Ident; +use syn::{Member, Path, spanned::Spanned}; /// Returns the correct path for `bevy_reflect`. pub(crate) fn get_bevy_reflect_path() -> Path { @@ -19,9 +19,11 @@ pub(crate) fn get_bevy_reflect_path() -> Path { /// let reflected: Ident = get_reflect_ident("Hash"); /// assert_eq!("ReflectHash", reflected.to_string()); /// ``` -pub(crate) fn get_reflect_ident(name: &str) -> Ident { - let reflected = format!("Reflect{name}"); - Ident::new(&reflected, Span::call_site()) +pub(crate) fn into_reflected_path(mut path: Path) -> Path { + let last = path.segments.last_mut().unwrap(); + let ident = Ident::new(&format!("Reflect{name}", name = last.ident.to_string()), last.span()); + last.ident = ident; + path } /// Helper struct used to process an iterator of `Result, syn::Error>`, diff --git a/crates/bevy_reflect/src/impls/glam.rs b/crates/bevy_reflect/src/impls/glam.rs index bb3ab96e3a365..42e6eae420c7f 100644 --- a/crates/bevy_reflect/src/impls/glam.rs +++ b/crates/bevy_reflect/src/impls/glam.rs @@ -5,14 +5,14 @@ use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_struct, impl_ref use glam::*; impl_reflect_struct!( - #[reflect(Debug, Hash, PartialEq, Default)] + #[reflect(debug, hash, partial_eq, Default)] struct IVec2 { x: i32, y: i32, } ); impl_reflect_struct!( - #[reflect(Debug, Hash, PartialEq, Default)] + #[reflect(debug, hash, partial_eq, Default)] struct IVec3 { x: i32, y: i32, @@ -20,7 +20,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, Hash, PartialEq, Default)] + #[reflect(debug, hash, partial_eq, Default)] struct IVec4 { x: i32, y: i32, @@ -30,14 +30,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, Hash, PartialEq, Default)] + #[reflect(debug, hash, partial_eq, Default)] struct UVec2 { x: u32, y: u32, } ); impl_reflect_struct!( - #[reflect(Debug, Hash, PartialEq, Default)] + #[reflect(debug, hash, partial_eq, Default)] struct UVec3 { x: u32, y: u32, @@ -45,7 +45,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, Hash, PartialEq, Default)] + #[reflect(debug, hash, partial_eq, Default)] struct UVec4 { x: u32, y: u32, @@ -55,14 +55,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Vec2 { x: f32, y: f32, } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Vec3 { x: f32, y: f32, @@ -70,7 +70,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Vec3A { x: f32, y: f32, @@ -78,7 +78,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Vec4 { x: f32, y: f32, @@ -88,14 +88,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct BVec2 { x: bool, y: bool, } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct BVec3 { x: bool, y: bool, @@ -103,7 +103,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct BVec4 { x: bool, y: bool, @@ -113,14 +113,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DVec2 { x: f64, y: f64, } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DVec3 { x: f64, y: f64, @@ -128,7 +128,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DVec4 { x: f64, y: f64, @@ -138,14 +138,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Mat2 { x_axis: Vec2, y_axis: Vec2, } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Mat3 { x_axis: Vec3, y_axis: Vec3, @@ -153,7 +153,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Mat3A { x_axis: Vec3A, y_axis: Vec3A, @@ -161,7 +161,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Mat4 { x_axis: Vec4, y_axis: Vec4, @@ -171,14 +171,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DMat2 { x_axis: DVec2, y_axis: DVec2, } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DMat3 { x_axis: DVec3, y_axis: DVec3, @@ -186,7 +186,7 @@ impl_reflect_struct!( } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DMat4 { x_axis: DVec4, y_axis: DVec4, @@ -196,14 +196,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Affine2 { matrix2: Mat2, translation: Vec2, } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct Affine3A { matrix3: Mat3A, translation: Vec3A, @@ -211,14 +211,14 @@ impl_reflect_struct!( ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DAffine2 { matrix2: DMat2, translation: DVec2, } ); impl_reflect_struct!( - #[reflect(Debug, PartialEq, Default)] + #[reflect(debug, partial_eq, Default)] struct DAffine3 { matrix3: DMat3, translation: DVec3, @@ -229,12 +229,12 @@ impl_reflect_struct!( // mechanisms for read-only fields. I doubt those mechanisms would be added, // so for now quaternions will remain as values. They are represented identically // to Vec4 and DVec4, so you may use those instead and convert between. -impl_reflect_value!(Quat(Debug, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(DQuat(Debug, PartialEq, Serialize, Deserialize, Default)); +impl_reflect_value!(Quat(debug, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(DQuat(debug, partial_eq, Serialize, Deserialize, Default)); impl_from_reflect_value!(Quat); impl_from_reflect_value!(DQuat); -impl_reflect_value!(EulerRot(Debug, Default)); -impl_reflect_value!(BVec3A(Debug, Default)); -impl_reflect_value!(BVec4A(Debug, Default)); +impl_reflect_value!(EulerRot(debug, Default)); +impl_reflect_value!(BVec3A(debug, Default)); +impl_reflect_value!(BVec4A(debug, Default)); diff --git a/crates/bevy_reflect/src/impls/rect.rs b/crates/bevy_reflect/src/impls/rect.rs index 9db5e1cd85196..5aa30af0ec045 100644 --- a/crates/bevy_reflect/src/impls/rect.rs +++ b/crates/bevy_reflect/src/impls/rect.rs @@ -5,7 +5,7 @@ use bevy_math::{Rect, Vec2}; use bevy_reflect_derive::impl_reflect_struct; impl_reflect_struct!( - #[reflect(Debug, PartialEq, Serialize, Deserialize, Default)] + #[reflect(debug, partial_eq, Serialize, Deserialize, Default)] struct Rect { min: Vec2, max: Vec2, diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 9b7bbf8ff77fa..9bafc32d2b6b4 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -27,75 +27,75 @@ use std::{ }; impl_reflect_value!(bool( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); impl_reflect_value!(char( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); -impl_reflect_value!(u8(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(u16(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(u32(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(u64(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); +impl_reflect_value!(u8(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(u16(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(u32(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(u64(debug, hash, partial_eq, Serialize, Deserialize, Default)); impl_reflect_value!(u128( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); impl_reflect_value!(usize( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); -impl_reflect_value!(i8(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(i16(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(i32(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(i64(Debug, Hash, PartialEq, Serialize, Deserialize, Default)); +impl_reflect_value!(i8(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(i16(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(i32(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(i64(debug, hash, partial_eq, Serialize, Deserialize, Default)); impl_reflect_value!(i128( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); impl_reflect_value!(isize( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); -impl_reflect_value!(f32(Debug, PartialEq, Serialize, Deserialize, Default)); -impl_reflect_value!(f64(Debug, PartialEq, Serialize, Deserialize, Default)); +impl_reflect_value!(f32(debug, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(f64(debug, partial_eq, Serialize, Deserialize, Default)); impl_reflect_value!(String( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); impl_reflect_value!(PathBuf( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default @@ -109,33 +109,33 @@ impl_reflect_value!(RangeTo()); impl_reflect_value!(RangeToInclusive()); impl_reflect_value!(RangeFull()); impl_reflect_value!(Duration( - Debug, - Hash, - PartialEq, + debug, + hash, + partial_eq, Serialize, Deserialize, Default )); -impl_reflect_value!(Instant(Debug, Hash, PartialEq)); -impl_reflect_value!(NonZeroI128(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroU128(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroIsize(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroUsize(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroI64(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroU64(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroU32(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroI32(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroI16(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroU16(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroU8(Debug, Hash, PartialEq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroI8(Debug, Hash, PartialEq, Serialize, Deserialize)); +impl_reflect_value!(Instant(debug, hash, partial_eq)); +impl_reflect_value!(NonZeroI128(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroU128(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroIsize(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroUsize(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroI64(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroU64(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroU32(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroI32(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroI16(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroU16(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroU8(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroI8(debug, hash, partial_eq, Serialize, Deserialize)); // `Serialize` and `Deserialize` only for platforms supported by serde: // https://github.com/serde-rs/serde/blob/3ffb86fc70efd3d329519e2dddfa306cc04f167c/serde/src/de/impls.rs#L1732 #[cfg(any(unix, windows))] -impl_reflect_value!(OsString(Debug, Hash, PartialEq, Serialize, Deserialize)); +impl_reflect_value!(OsString(debug, hash, partial_eq, Serialize, Deserialize)); #[cfg(not(any(unix, windows)))] -impl_reflect_value!(OsString(Debug, Hash, PartialEq)); +impl_reflect_value!(OsString(debug, hash, partial_eq)); impl_from_reflect_value!(bool); impl_from_reflect_value!(char); diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index dcf2d85861722..cfad93e968cd6 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -161,7 +161,7 @@ mod tests { #[test] fn reflect_map() { #[derive(Reflect, Hash)] - #[reflect(Hash)] + #[reflect(hash)] struct Foo { a: u32, b: String, @@ -346,7 +346,7 @@ mod tests { #[test] fn reflect_complex_patch() { #[derive(Reflect, Eq, PartialEq, Debug, FromReflect)] - #[reflect(PartialEq)] + #[reflect(partial_eq)] struct Foo { a: u32, #[reflect(ignore)] @@ -360,7 +360,7 @@ mod tests { } #[derive(Reflect, Eq, PartialEq, Clone, Debug, FromReflect)] - #[reflect(PartialEq)] + #[reflect(partial_eq)] struct Bar { x: u32, } @@ -594,7 +594,7 @@ mod tests { #[test] fn reflect_take() { #[derive(Reflect, Debug, PartialEq)] - #[reflect(PartialEq)] + #[reflect(partial_eq)] struct Bar { x: u32, } @@ -1093,7 +1093,7 @@ mod tests { struct SomeTupleStruct(String); #[derive(Reflect)] - #[reflect(Debug)] + #[reflect(debug)] struct CustomDebug; impl Debug for CustomDebug { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { @@ -1159,8 +1159,8 @@ bevy_reflect::tests::should_reflect_debug::Test { #[test] fn multiple_reflect_lists() { #[derive(Hash, PartialEq, Reflect)] - #[reflect(Debug, Hash)] - #[reflect(PartialEq)] + #[reflect(debug, hash)] + #[reflect(partial_eq)] struct Foo(i32); impl Debug for Foo { @@ -1180,8 +1180,7 @@ bevy_reflect::tests::should_reflect_debug::Test { #[test] fn multiple_reflect_value_lists() { #[derive(Clone, Hash, PartialEq, Reflect)] - #[reflect_value(Debug, Hash)] - #[reflect_value(PartialEq)] + #[reflect_value(debug, hash, partial_eq)] struct Foo(i32); impl Debug for Foo { diff --git a/crates/bevy_reflect/src/serde/mod.rs b/crates/bevy_reflect/src/serde/mod.rs index 3355ed38d0b11..4e3a9b24b65c3 100644 --- a/crates/bevy_reflect/src/serde/mod.rs +++ b/crates/bevy_reflect/src/serde/mod.rs @@ -19,7 +19,7 @@ mod tests { #[test] fn test_serialization_struct() { #[derive(Debug, Reflect, PartialEq)] - #[reflect(PartialEq)] + #[reflect(partial_eq)] struct TestStruct { a: i32, #[reflect(ignore)] @@ -61,7 +61,7 @@ mod tests { #[test] fn test_serialization_tuple_struct() { #[derive(Debug, Reflect, PartialEq)] - #[reflect(PartialEq)] + #[reflect(partial_eq)] struct TestStruct( i32, #[reflect(ignore)] i32, diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 18817aec03f3f..92daa70fddd17 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -10,7 +10,7 @@ use std::ops::{Add, AddAssign, Mul, MulAssign}; use thiserror::Error; #[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Reflect, FromReflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum Color { /// sRGBA color Rgba { diff --git a/crates/bevy_render/src/view/visibility/render_layers.rs b/crates/bevy_render/src/view/visibility/render_layers.rs index 677c0877777ba..d861371fd1f9a 100644 --- a/crates/bevy_render/src/view/visibility/render_layers.rs +++ b/crates/bevy_render/src/view/visibility/render_layers.rs @@ -21,7 +21,7 @@ pub type Layer = u8; /// /// Entities without this component belong to layer `0`. #[derive(Component, Copy, Clone, Reflect, PartialEq, Eq, PartialOrd, Ord)] -#[reflect(Component, Default, PartialEq)] +#[reflect(Component, Default, partial_eq)] pub struct RenderLayers(LayerMask); impl std::fmt::Debug for RenderLayers { diff --git a/crates/bevy_sprite/src/mesh2d/color_material.rs b/crates/bevy_sprite/src/mesh2d/color_material.rs index 57c57deb2bc2c..fdd7d9482d2cb 100644 --- a/crates/bevy_sprite/src/mesh2d/color_material.rs +++ b/crates/bevy_sprite/src/mesh2d/color_material.rs @@ -40,7 +40,7 @@ impl Plugin for ColorMaterialPlugin { /// A [2d material](Material2d) that renders [2d meshes](crate::Mesh2dHandle) with a texture tinted by a uniform color #[derive(AsBindGroup, Reflect, FromReflect, Debug, Clone, TypeUuid)] -#[reflect(Default, Debug)] +#[reflect(debug, Default)] #[uuid = "e228a544-e3ca-4e1e-bb9d-4d8bc1ad8c19"] #[uniform(0, ColorMaterialUniform)] pub struct ColorMaterial { diff --git a/crates/bevy_sprite/src/texture_atlas.rs b/crates/bevy_sprite/src/texture_atlas.rs index ae9891914fe3d..f519f48f5ee6b 100644 --- a/crates/bevy_sprite/src/texture_atlas.rs +++ b/crates/bevy_sprite/src/texture_atlas.rs @@ -11,7 +11,7 @@ use bevy_utils::HashMap; /// [Example usage loading sprite sheet.](https://github.com/bevyengine/bevy/blob/latest/examples/2d/texture_atlas.rs) #[derive(Reflect, FromReflect, Debug, Clone, TypeUuid)] #[uuid = "7233c597-ccfa-411f-bd59-9af349432ada"] -#[reflect(Debug)] +#[reflect(debug)] pub struct TextureAtlas { /// The handle to the texture in which the sprites are stored pub texture: Handle, diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 71355f2693285..d2e8a5e06c622 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -38,7 +38,7 @@ use bevy_reflect::{std_traits::ReflectDefault, FromReflect, Reflect}; /// [`transform`]: https://github.com/bevyengine/bevy/blob/latest/examples/transforms/transform.rs #[derive(Component, Debug, PartialEq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[reflect(Component, Default, PartialEq)] +#[reflect(partial_eq, Component, Default)] pub struct GlobalTransform(Affine3A); macro_rules! impl_local_axis { diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index 1a437788ebfb1..423c58a557728 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -36,7 +36,7 @@ use std::ops::Mul; /// [`transform`]: https://github.com/bevyengine/bevy/blob/latest/examples/transforms/transform.rs #[derive(Component, Debug, PartialEq, Clone, Copy, Reflect, FromReflect)] #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))] -#[reflect(Component, Default, PartialEq)] +#[reflect(Component, Default, partial_eq)] pub struct Transform { /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering. /// diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index b30062818dac1..a09afd5a83424 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -33,7 +33,7 @@ use smallvec::SmallVec; /// Note that you can also control the visibility of a node using the [`Display`](crate::ui_node::Display) property, /// which fully collapses it during layout calculations. #[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(Component, Serialize, Deserialize, PartialEq)] +#[reflect(partial_eq, Component, Serialize, Deserialize)] pub enum Interaction { /// The node has been clicked Clicked, @@ -71,7 +71,7 @@ impl Default for Interaction { Serialize, Deserialize, )] -#[reflect(Component, Serialize, Deserialize, PartialEq)] +#[reflect(partial_eq, Component, Serialize, Deserialize)] pub struct RelativeCursorPosition { /// Cursor position relative to size and position of the Node. pub normalized: Option, @@ -88,7 +88,7 @@ impl RelativeCursorPosition { /// Describes whether the node should block interactions with lower nodes #[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(Component, Serialize, Deserialize, PartialEq)] +#[reflect(partial_eq, Component, Serialize, Deserialize)] pub enum FocusPolicy { /// Blocks interaction Block, diff --git a/crates/bevy_ui/src/geometry.rs b/crates/bevy_ui/src/geometry.rs index 42eaa5651c665..01d43d197afde 100644 --- a/crates/bevy_ui/src/geometry.rs +++ b/crates/bevy_ui/src/geometry.rs @@ -120,7 +120,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign}; /// }; /// ``` #[derive(Copy, Clone, PartialEq, Debug, Reflect)] -#[reflect(PartialEq)] +#[reflect(partial_eq)] pub struct UiRect { /// The value corresponding to the left side of the UI rect. pub left: Val, @@ -330,7 +330,7 @@ impl Default for UiRect { /// /// It is commonly used to define the size of a text or UI element. #[derive(Copy, Clone, PartialEq, Debug, Reflect)] -#[reflect(PartialEq)] +#[reflect(partial_eq)] pub struct Size { /// The width of the 2-dimensional area. pub width: Val, diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index 31f232c4d1d85..f28efb529fa41 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -42,7 +42,7 @@ impl Default for Node { /// An enum that describes possible types of value in flexbox layout options #[derive(Copy, Clone, PartialEq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum Val { /// No value defined Undefined, @@ -211,7 +211,7 @@ impl Val { /// /// It uses the [Flexbox](https://cssreference.io/flexbox/) system. #[derive(Component, Clone, PartialEq, Debug, Reflect)] -#[reflect(Component, Default, PartialEq)] +#[reflect(partial_eq, Component, Default)] pub struct Style { /// Whether to arrange this node and its children with flexbox layout /// @@ -299,7 +299,7 @@ impl Default for Style { /// How items are aligned according to the cross axis #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum AlignItems { /// Items are aligned at the start FlexStart, @@ -325,7 +325,7 @@ impl Default for AlignItems { /// Works like [`AlignItems`] but applies only to a single item #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum AlignSelf { /// Use the value of [`AlignItems`] Auto, @@ -355,7 +355,7 @@ impl Default for AlignSelf { /// /// It only applies if [`FlexWrap::Wrap`] is present and if there are multiple lines of items. #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum AlignContent { /// Each line moves towards the start of the cross axis FlexStart, @@ -387,7 +387,7 @@ impl Default for AlignContent { /// /// For example English is written LTR (left-to-right) while Arabic is written RTL (right-to-left). #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum Direction { /// Inherit from parent node Inherit, @@ -411,7 +411,7 @@ impl Default for Direction { /// /// Part of the [`Style`] component. #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum Display { /// Use Flexbox layout model to determine the position of this [`Node`]. Flex, @@ -434,7 +434,7 @@ impl Default for Display { /// Defines how flexbox items are ordered within a flexbox #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum FlexDirection { /// Same way as text direction along the main axis Row, @@ -458,7 +458,7 @@ impl Default for FlexDirection { /// Defines how items are aligned according to the main axis #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum JustifyContent { /// Pushed towards the start FlexStart, @@ -486,7 +486,7 @@ impl Default for JustifyContent { /// Whether to show or hide overflowing items #[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect, Serialize, Deserialize)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum Overflow { /// Show overflowing items Visible, @@ -506,7 +506,7 @@ impl Default for Overflow { /// The strategy used to position this node #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum PositionType { /// Relative to all other nodes with the [`PositionType::Relative`] value Relative, @@ -528,7 +528,7 @@ impl Default for PositionType { /// Defines if flexbox items appear on a single line or on multiple lines #[derive(Copy, Clone, PartialEq, Eq, Debug, Serialize, Deserialize, Reflect)] -#[reflect(PartialEq, Serialize, Deserialize)] +#[reflect(partial_eq, Serialize, Deserialize)] pub enum FlexWrap { /// Single line, will overflow if needed NoWrap, diff --git a/crates/bevy_window/src/cursor.rs b/crates/bevy_window/src/cursor.rs index 17e4dce56b892..f448f546edd4a 100644 --- a/crates/bevy_window/src/cursor.rs +++ b/crates/bevy_window/src/cursor.rs @@ -14,7 +14,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq, Default)] +#[reflect(debug, partial_eq, Default)] pub enum CursorIcon { /// The platform-dependent default cursor. #[default] diff --git a/crates/bevy_window/src/event.rs b/crates/bevy_window/src/event.rs index 0141420020811..7fad802623d0a 100644 --- a/crates/bevy_window/src/event.rs +++ b/crates/bevy_window/src/event.rs @@ -9,7 +9,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize}; /// A window event that is sent whenever a window's logical size has changed. #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -28,7 +28,7 @@ pub struct WindowResized { /// An event that indicates the window should redraw, even if its control flow is set to `Wait` and /// there have been no window events. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -40,7 +40,7 @@ pub struct RequestRedraw; /// /// To create a new window, spawn an entity with a [`crate::Window`] on it. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -62,7 +62,7 @@ pub struct WindowCreated { /// [`WindowPlugin`]: crate::WindowPlugin /// [`Window`]: crate::Window #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -76,7 +76,7 @@ pub struct WindowCloseRequested { /// An event that is sent whenever a window is closed. This will be sent when /// the window entity loses its `Window` component or is despawned. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -99,7 +99,7 @@ pub struct WindowClosed { /// [`WindowEvent::CursorMoved`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.CursorMoved /// [`MouseMotion`]: bevy_input::mouse::MouseMotion #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -114,7 +114,7 @@ pub struct CursorMoved { /// An event that is sent whenever the user's cursor enters a window. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -127,7 +127,7 @@ pub struct CursorEntered { /// An event that is sent whenever the user's cursor leaves a window. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -140,7 +140,7 @@ pub struct CursorLeft { /// An event that is sent whenever a window receives a character from the OS or underlying system. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -155,7 +155,7 @@ pub struct ReceivedCharacter { /// An event that indicates a window has received or lost focus. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -170,7 +170,7 @@ pub struct WindowFocused { /// An event that indicates a window's scale factor has changed. #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -185,7 +185,7 @@ pub struct WindowScaleFactorChanged { /// An event that indicates a window's OS-reported scale factor has changed. #[derive(Debug, Clone, PartialEq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -200,7 +200,7 @@ pub struct WindowBackendScaleFactorChanged { /// Events related to files being dragged and dropped on a window. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), @@ -232,7 +232,7 @@ pub enum FileDragAndDrop { /// An event that is sent when a window is repositioned in physical pixels. #[derive(Debug, Clone, PartialEq, Eq, Reflect, FromReflect)] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] #[cfg_attr( feature = "serialize", derive(serde::Serialize, serde::Deserialize), diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index ec29a7e9692aa..af284fb9a27a3 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -270,7 +270,7 @@ impl Window { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq, Default)] +#[reflect(debug, partial_eq, Default)] pub struct WindowResizeConstraints { /// The minimum width the window can have. pub min_width: f32, @@ -337,7 +337,7 @@ impl WindowResizeConstraints { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, Default)] +#[reflect(debug, Default)] pub struct Cursor { /// Get the current [`CursorIcon`] while inside the window. pub icon: CursorIcon, @@ -393,7 +393,7 @@ impl Default for Cursor { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] pub enum WindowPosition { /// Position will be set by the window manager #[default] @@ -444,7 +444,7 @@ impl WindowPosition { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq, Default)] +#[reflect(debug, partial_eq, Default)] pub struct WindowResolution { physical_width: u32, physical_height: u32, @@ -608,7 +608,7 @@ impl From for WindowResolution { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq, Default)] +#[reflect(debug, partial_eq, Default)] pub enum CursorGrabMode { /// The cursor can freely leave the window. #[default] @@ -626,7 +626,7 @@ pub enum CursorGrabMode { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq, Default)] +#[reflect(debug, partial_eq, Default)] pub struct InternalWindowState { /// If this is true then next frame we will ask to minimize the window. minimize_request: Option, @@ -653,7 +653,7 @@ impl InternalWindowState { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] pub enum MonitorSelection { /// Uses current monitor of the window. /// @@ -684,7 +684,7 @@ pub enum MonitorSelection { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq, Hash)] +#[reflect(debug, partial_eq, hash)] #[doc(alias = "vsync")] pub enum PresentMode { /// Chooses FifoRelaxed -> Fifo based on availability. @@ -724,7 +724,7 @@ pub enum PresentMode { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq, Hash)] +#[reflect(debug, partial_eq, hash)] pub enum CompositeAlphaMode { /// Chooses either `Opaque` or `Inherit` automatically, depending on the /// `alpha_mode` that the current surface can support. @@ -759,7 +759,7 @@ pub enum CompositeAlphaMode { derive(serde::Serialize, serde::Deserialize), reflect(Serialize, Deserialize) )] -#[reflect(Debug, PartialEq)] +#[reflect(debug, partial_eq)] pub enum WindowMode { /// Creates a window that uses the given size. #[default] diff --git a/examples/reflection/reflection_types.rs b/examples/reflection/reflection_types.rs index db9dc4b61d995..9802fd3cb4ce5 100644 --- a/examples/reflection/reflection_types.rs +++ b/examples/reflection/reflection_types.rs @@ -45,7 +45,7 @@ pub enum D { /// `Reflect::serializable()`. You can force these implementations to use the actual trait /// implementations (instead of their defaults) like this: #[derive(Reflect, Hash, Serialize, PartialEq, Eq)] -#[reflect(Hash, Serialize, PartialEq)] +#[reflect(hash, partial_eq, Serialize)] pub struct E { x: usize, } @@ -56,7 +56,7 @@ pub struct E { /// the `PartialEq`, `Serialize`, and `Deserialize` traits on `reflect_value` types to ensure /// that these values behave as expected when nested underneath Reflect-ed structs. #[derive(Reflect, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)] -#[reflect_value(PartialEq, Serialize, Deserialize)] +#[reflect_value(partial_eq, Serialize, Deserialize)] pub enum F { X, Y, From 98a67d44f417737480788c9ce8c118a34e6996c1 Mon Sep 17 00:00:00 2001 From: radiish Date: Sat, 21 Jan 2023 15:29:05 +0000 Subject: [PATCH 2/9] `cargo fmt --all` --- .../src/container_attributes.rs | 18 +++-- .../bevy_reflect_derive/src/derive_data.rs | 12 +++- .../bevy_reflect_derive/src/from_reflect.rs | 5 +- .../bevy_reflect_derive/src/lib.rs | 2 +- .../src/trait_reflection.rs | 2 +- .../bevy_reflect_derive/src/utility.rs | 7 +- crates/bevy_reflect/src/impls/std.rs | 70 ++++++++++++++++--- 7 files changed, 94 insertions(+), 22 deletions(-) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs index cbb0bcb00830a..4be68194eb50f 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs @@ -13,7 +13,7 @@ use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::token::Comma; -use syn::{Meta, NestedMeta, Path, Lit, parse_str}; +use syn::{parse_str, Lit, Meta, NestedMeta, Path}; // The "special" idents that are used internally for reflection. // Received via attributes like `#[reflect(partial_eq, hash, ...)]` @@ -136,7 +136,12 @@ impl ReflectTraits { // Track the span where the trait is implemented for future errors let span = lit.span(); let trait_func_ident = TraitImpl::Custom(path, span); - match name_value.path.get_ident().map(ToString::to_string).as_deref() { + match name_value + .path + .get_ident() + .map(ToString::to_string) + .as_deref() + { Some(DEBUG_ATTR) => { traits.debug = traits.debug.merge(trait_func_ident)?; } @@ -147,11 +152,14 @@ impl ReflectTraits { traits.hash = traits.hash.merge(trait_func_ident)?; } _ => { - return Err(syn::Error::new(span, "custom path literals can only be used for \"special\" idents.")) + return Err(syn::Error::new( + span, + "custom path literals can only be used for \"special\" idents.", + )) } } } - }, + } // Handles `#[reflect( hash, Default, ... )]` NestedMeta::Meta(Meta::Path(path)) => { // Track the span where the trait is implemented for future errors @@ -279,4 +287,4 @@ impl Parse for ReflectTraits { let result = Punctuated::::parse_terminated(input)?; ReflectTraits::from_nested_metas(&result) } -} \ No newline at end of file +} diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs b/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs index 99f65dbac2310..8394e756ae27b 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/derive_data.rs @@ -137,7 +137,10 @@ enum ReflectMode { } impl<'a> ReflectDerive<'a> { - pub fn from_input(input: &'a DeriveInput, impl_source: ReflectImplSource) -> Result { + pub fn from_input( + input: &'a DeriveInput, + impl_source: ReflectImplSource, + ) -> Result { let mut traits = ReflectTraits::default(); // Should indicate whether `#[reflect_value]` was used let mut reflect_mode = None; @@ -288,7 +291,12 @@ impl<'a> ReflectDerive<'a> { } impl<'a> ReflectMeta<'a> { - pub fn new(type_name: &'a Ident, generics: &'a Generics, traits: ReflectTraits, impl_source: ReflectImplSource) -> Self { + pub fn new( + type_name: &'a Ident, + generics: &'a Generics, + traits: ReflectTraits, + impl_source: ReflectImplSource, + ) -> Self { Self { traits, type_name, diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs b/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs index 38ac9a7ccf699..a5f20e5eeae41 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs @@ -91,7 +91,7 @@ fn impl_struct_internal(reflect_struct: &ReflectStruct, is_tuple: bool) -> Token let MemberValuePair(ignored_members, ignored_values) = get_ignored_fields(reflect_struct, is_tuple); - + let constructor = match reflect_struct.meta().impl_source() { ReflectImplSource::ImplStruct => { quote!( @@ -107,9 +107,8 @@ fn impl_struct_internal(reflect_struct: &ReflectStruct, is_tuple: bool) -> Token )* #FQOption::Some(__this) ) - }, + } _ => { - quote!( #FQOption::Some( Self { diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs index 65808a075533c..ea78dfc4020cb 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs @@ -103,7 +103,7 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream { &def.type_name, &def.generics, def.traits.unwrap_or_default(), - ReflectImplSource::ImplValue + ReflectImplSource::ImplValue, ); #[cfg(feature = "documentation")] diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs b/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs index 4673fd4b80ebd..c2c279aa7e6bd 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/trait_reflection.rs @@ -2,7 +2,7 @@ use crate::fq_std::{FQBox, FQClone, FQOption, FQResult}; use bevy_macro_utils::BevyManifest; use proc_macro::TokenStream; use quote::quote; -use syn::{parse::Parse, parse_macro_input, Attribute, ItemTrait, Token, Path}; +use syn::{parse::Parse, parse_macro_input, Attribute, ItemTrait, Path, Token}; pub(crate) struct TraitInfo { item_trait: ItemTrait, diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs index 9e19a0359bc1b..09054e9feb492 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs @@ -4,7 +4,7 @@ use crate::field_attributes::ReflectIgnoreBehavior; use bevy_macro_utils::BevyManifest; use bit_set::BitSet; use proc_macro2::Ident; -use syn::{Member, Path, spanned::Spanned}; +use syn::{spanned::Spanned, Member, Path}; /// Returns the correct path for `bevy_reflect`. pub(crate) fn get_bevy_reflect_path() -> Path { @@ -21,7 +21,10 @@ pub(crate) fn get_bevy_reflect_path() -> Path { /// ``` pub(crate) fn into_reflected_path(mut path: Path) -> Path { let last = path.segments.last_mut().unwrap(); - let ident = Ident::new(&format!("Reflect{name}", name = last.ident.to_string()), last.span()); + let ident = Ident::new( + &format!("Reflect{name}", name = last.ident.to_string()), + last.span(), + ); last.ident = ident; path } diff --git a/crates/bevy_reflect/src/impls/std.rs b/crates/bevy_reflect/src/impls/std.rs index 9bafc32d2b6b4..1103175c803d3 100644 --- a/crates/bevy_reflect/src/impls/std.rs +++ b/crates/bevy_reflect/src/impls/std.rs @@ -43,9 +43,30 @@ impl_reflect_value!(char( Default )); impl_reflect_value!(u8(debug, hash, partial_eq, Serialize, Deserialize, Default)); -impl_reflect_value!(u16(debug, hash, partial_eq, Serialize, Deserialize, Default)); -impl_reflect_value!(u32(debug, hash, partial_eq, Serialize, Deserialize, Default)); -impl_reflect_value!(u64(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(u16( + debug, + hash, + partial_eq, + Serialize, + Deserialize, + Default +)); +impl_reflect_value!(u32( + debug, + hash, + partial_eq, + Serialize, + Deserialize, + Default +)); +impl_reflect_value!(u64( + debug, + hash, + partial_eq, + Serialize, + Deserialize, + Default +)); impl_reflect_value!(u128( debug, hash, @@ -63,9 +84,30 @@ impl_reflect_value!(usize( Default )); impl_reflect_value!(i8(debug, hash, partial_eq, Serialize, Deserialize, Default)); -impl_reflect_value!(i16(debug, hash, partial_eq, Serialize, Deserialize, Default)); -impl_reflect_value!(i32(debug, hash, partial_eq, Serialize, Deserialize, Default)); -impl_reflect_value!(i64(debug, hash, partial_eq, Serialize, Deserialize, Default)); +impl_reflect_value!(i16( + debug, + hash, + partial_eq, + Serialize, + Deserialize, + Default +)); +impl_reflect_value!(i32( + debug, + hash, + partial_eq, + Serialize, + Deserialize, + Default +)); +impl_reflect_value!(i64( + debug, + hash, + partial_eq, + Serialize, + Deserialize, + Default +)); impl_reflect_value!(i128( debug, hash, @@ -119,8 +161,20 @@ impl_reflect_value!(Duration( impl_reflect_value!(Instant(debug, hash, partial_eq)); impl_reflect_value!(NonZeroI128(debug, hash, partial_eq, Serialize, Deserialize)); impl_reflect_value!(NonZeroU128(debug, hash, partial_eq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroIsize(debug, hash, partial_eq, Serialize, Deserialize)); -impl_reflect_value!(NonZeroUsize(debug, hash, partial_eq, Serialize, Deserialize)); +impl_reflect_value!(NonZeroIsize( + debug, + hash, + partial_eq, + Serialize, + Deserialize +)); +impl_reflect_value!(NonZeroUsize( + debug, + hash, + partial_eq, + Serialize, + Deserialize +)); impl_reflect_value!(NonZeroI64(debug, hash, partial_eq, Serialize, Deserialize)); impl_reflect_value!(NonZeroU64(debug, hash, partial_eq, Serialize, Deserialize)); impl_reflect_value!(NonZeroU32(debug, hash, partial_eq, Serialize, Deserialize)); From 4f28dc2fc19110e1c0a8e4dd6de89ad960366b82 Mon Sep 17 00:00:00 2001 From: radiish Date: Sat, 21 Jan 2023 15:44:34 +0000 Subject: [PATCH 3/9] use curly braces --- .../bevy_reflect_derive/src/from_reflect.rs | 8 ++++---- crates/bevy_reflect/bevy_reflect_derive/src/lib.rs | 5 ----- .../bevy_reflect/bevy_reflect_derive/src/utility.rs | 11 ++++------- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs b/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs index a5f20e5eeae41..5cc8b0148ac6c 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/from_reflect.rs @@ -94,7 +94,7 @@ fn impl_struct_internal(reflect_struct: &ReflectStruct, is_tuple: bool) -> Token let constructor = match reflect_struct.meta().impl_source() { ReflectImplSource::ImplStruct => { - quote!( + quote! { // This is necessary for `impl_reflect_struct` on types like `Vec4` // because on some targets field access is implemented using `Deref(Mut)`. let mut __this: Self = #FQDefault::default(); @@ -106,17 +106,17 @@ fn impl_struct_internal(reflect_struct: &ReflectStruct, is_tuple: bool) -> Token __this.#ignored_members = #ignored_values; )* #FQOption::Some(__this) - ) + } } _ => { - quote!( + quote! { #FQOption::Some( Self { #(#active_members: #active_values()?,)* #(#ignored_members: #ignored_values,)* } ) - ) + } } }; diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs index ea78dfc4020cb..6e1c54be18fad 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs @@ -121,11 +121,6 @@ pub fn impl_reflect_value(input: TokenStream) -> TokenStream { /// which have greater functionality. The type being reflected must be in scope, as you cannot /// qualify it in the macro as e.g. `bevy::prelude::Vec3`. /// -/// It may be necessary to add `#[reflect(Default)]` for some types, specifically non-constructible -/// foreign types. Without `Default` reflected for such types, you will usually get an arcane -/// error message and fail to compile. If the type does not implement `Default`, it may not -/// be possible to reflect without extending the macro. -/// /// # Example /// Implementing `Reflect` for `bevy::prelude::Vec3` as a struct type: /// ```ignore diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs index 09054e9feb492..713cef962dce6 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/utility.rs @@ -11,20 +11,17 @@ pub(crate) fn get_bevy_reflect_path() -> Path { BevyManifest::get_path_direct("bevy_reflect") } -/// Returns the "reflected" ident for a given string. +/// Returns the "reflected" path for a given path. /// /// # Example /// /// ```ignore -/// let reflected: Ident = get_reflect_ident("Hash"); -/// assert_eq!("ReflectHash", reflected.to_string()); +/// let path: Path = parse_str("my_crate::MyTrait"); +/// let reflected = into_reflect_path(path); // == "my_crate::ReflectMyTrait" /// ``` pub(crate) fn into_reflected_path(mut path: Path) -> Path { let last = path.segments.last_mut().unwrap(); - let ident = Ident::new( - &format!("Reflect{name}", name = last.ident.to_string()), - last.span(), - ); + let ident = Ident::new(&format!("Reflect{name}", name = last.ident), last.span()); last.ident = ident; path } From ba56ef91842b270f52e294d52246f02be502add5 Mon Sep 17 00:00:00 2001 From: radiish Date: Sat, 21 Jan 2023 16:36:56 +0000 Subject: [PATCH 4/9] improve docs --- .../src/container_attributes.rs | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs index 4be68194eb50f..dc1a225587a9f 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs @@ -15,36 +15,36 @@ use syn::spanned::Spanned; use syn::token::Comma; use syn::{parse_str, Lit, Meta, NestedMeta, Path}; -// The "special" idents that are used internally for reflection. +// The "special" trait-like idents that are used internally for reflection. // Received via attributes like `#[reflect(partial_eq, hash, ...)]` const DEBUG_ATTR: &str = "debug"; const PARTIAL_EQ_ATTR: &str = "partial_eq"; const HASH_ATTR: &str = "hash"; -/// A marker for trait implementations registered via the `Reflect` derive macro. +/// A marker for special trait-like ident implementations registered via the `Reflect` derive macro. #[derive(Clone, Default)] -pub(crate) enum TraitImpl { - /// The trait is not registered as implemented. +pub(crate) enum TraitLikeImpl { + /// The ident is not registered as implemented. #[default] NotImplemented, - /// The trait is registered as implemented. + /// The ident is registered as implemented. Implemented(Span), - /// The trait is registered with a custom function rather than an actual implementation. + /// The ident is registered with a custom function rather than the trait's implementation. Custom(Path, Span), } -impl TraitImpl { - /// Merges this [`TraitImpl`] with another. +impl TraitLikeImpl { + /// Merges this [`TraitLikeImpl`] with another. /// - /// Returns whichever value is not [`TraitImpl::NotImplemented`]. - /// If both values are [`TraitImpl::NotImplemented`], then that is returned. - /// Otherwise, an error is returned if neither value is [`TraitImpl::NotImplemented`]. - pub fn merge(self, other: TraitImpl) -> Result { + /// Returns whichever value is not [`TraitLikeImpl::NotImplemented`]. + /// If both values are [`TraitLikeImpl::NotImplemented`], then that is returned. + /// Otherwise, an error is returned if neither value is [`TraitLikeImpl::NotImplemented`]. + pub fn merge(self, other: TraitLikeImpl) -> Result { match (self, other) { - (TraitImpl::NotImplemented, value) | (value, TraitImpl::NotImplemented) => Ok(value), - (_, TraitImpl::Implemented(span) | TraitImpl::Custom(_, span)) => { + (TraitLikeImpl::NotImplemented, value) | (value, TraitLikeImpl::NotImplemented) => Ok(value), + (_, TraitLikeImpl::Implemented(span) | TraitLikeImpl::Custom(_, span)) => { Err(syn::Error::new(span, "conflicting type data registration")) } } @@ -54,11 +54,11 @@ impl TraitImpl { /// A collection of traits that have been registered for a reflected type. /// /// This keeps track of a few traits that are utilized internally for reflection -/// (we'll call these traits _special traits_ within this context), but it +/// (we'll call these traits _special trait-like idents_ within this context), but it /// will also keep track of all registered traits. Traits are registered as part of the /// `Reflect` derive macro using the helper attribute: `#[reflect(...)]`. /// -/// The list of special traits are as follows: +/// The list of special trait-like idents are as follows: /// * `debug` /// * `hash` /// * `partial_eq` @@ -88,7 +88,7 @@ impl TraitImpl { /// Registering `hash` using the `Hash` trait: /// /// ```ignore -/// // `hash` is a "special trait" and does not need (nor have) a ReflectHash struct +/// // `hash` is "special" and does not need (nor have) a ReflectHash struct /// /// #[derive(Reflect, Hash)] /// #[reflect(hash)] @@ -114,9 +114,9 @@ impl TraitImpl { /// #[derive(Default, Clone)] pub(crate) struct ReflectTraits { - debug: TraitImpl, - hash: TraitImpl, - partial_eq: TraitImpl, + debug: TraitLikeImpl, + hash: TraitLikeImpl, + partial_eq: TraitLikeImpl, idents: Vec, } @@ -135,7 +135,7 @@ impl ReflectTraits { let path: Path = parse_str(&lit.value())?; // Track the span where the trait is implemented for future errors let span = lit.span(); - let trait_func_ident = TraitImpl::Custom(path, span); + let trait_func_ident = TraitLikeImpl::Custom(path, span); match name_value .path .get_ident() @@ -167,14 +167,14 @@ impl ReflectTraits { match path.get_ident().map(ToString::to_string).as_deref() { Some(DEBUG_ATTR) => { - traits.debug = traits.debug.merge(TraitImpl::Implemented(span))?; + traits.debug = traits.debug.merge(TraitLikeImpl::Implemented(span))?; } Some(PARTIAL_EQ_ATTR) => { traits.partial_eq = - traits.partial_eq.merge(TraitImpl::Implemented(span))?; + traits.partial_eq.merge(TraitLikeImpl::Implemented(span))?; } Some(HASH_ATTR) => { - traits.hash = traits.hash.merge(TraitImpl::Implemented(span))?; + traits.hash = traits.hash.merge(TraitLikeImpl::Implemented(span))?; } _ => { // Create the reflect ident @@ -201,7 +201,7 @@ impl ReflectTraits { /// If `hash` was not registered, returns `None`. pub fn get_hash_impl(&self, bevy_reflect_path: &Path) -> Option { match &self.hash { - &TraitImpl::Implemented(span) => Some(quote_spanned! {span=> + &TraitLikeImpl::Implemented(span) => Some(quote_spanned! {span=> fn reflect_hash(&self) -> #FQOption { use ::core::hash::{Hash, Hasher}; let mut hasher: #bevy_reflect_path::ReflectHasher = #FQDefault::default(); @@ -210,12 +210,12 @@ impl ReflectTraits { #FQOption::Some(Hasher::finish(&hasher)) } }), - &TraitImpl::Custom(ref impl_fn, span) => Some(quote_spanned! {span=> + &TraitLikeImpl::Custom(ref impl_fn, span) => Some(quote_spanned! {span=> fn reflect_hash(&self) -> #FQOption { #FQOption::Some(#impl_fn(self)) } }), - TraitImpl::NotImplemented => None, + TraitLikeImpl::NotImplemented => None, } } @@ -227,7 +227,7 @@ impl ReflectTraits { bevy_reflect_path: &Path, ) -> Option { match &self.partial_eq { - &TraitImpl::Implemented(span) => Some(quote_spanned! {span=> + &TraitLikeImpl::Implemented(span) => Some(quote_spanned! {span=> fn reflect_partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> #FQOption { let value = ::as_any(value); if let #FQOption::Some(value) = ::downcast_ref::(value) { @@ -237,12 +237,12 @@ impl ReflectTraits { } } }), - &TraitImpl::Custom(ref impl_fn, span) => Some(quote_spanned! {span=> + &TraitLikeImpl::Custom(ref impl_fn, span) => Some(quote_spanned! {span=> fn reflect_partial_eq(&self, value: &dyn #bevy_reflect_path::Reflect) -> #FQOption { #FQOption::Some(#impl_fn(self, value)) } }), - TraitImpl::NotImplemented => None, + TraitLikeImpl::NotImplemented => None, } } @@ -251,17 +251,17 @@ impl ReflectTraits { /// If `debug` was not registered, returns `None`. pub fn get_debug_impl(&self) -> Option { match &self.debug { - &TraitImpl::Implemented(span) => Some(quote_spanned! {span=> + &TraitLikeImpl::Implemented(span) => Some(quote_spanned! {span=> fn debug(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { ::core::fmt::Debug::fmt(self, f) } }), - &TraitImpl::Custom(ref impl_fn, span) => Some(quote_spanned! {span=> + &TraitLikeImpl::Custom(ref impl_fn, span) => Some(quote_spanned! {span=> fn debug(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { #impl_fn(self, f) } }), - TraitImpl::NotImplemented => None, + TraitLikeImpl::NotImplemented => None, } } From 650b8b7c0849d1fe3169bfd697f9758fcd074c80 Mon Sep 17 00:00:00 2001 From: radiish Date: Sat, 21 Jan 2023 16:39:05 +0000 Subject: [PATCH 5/9] `cargo fmt --all` --- .../bevy_reflect_derive/src/container_attributes.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs index dc1a225587a9f..0e9a5427a54a2 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs @@ -43,7 +43,9 @@ impl TraitLikeImpl { /// Otherwise, an error is returned if neither value is [`TraitLikeImpl::NotImplemented`]. pub fn merge(self, other: TraitLikeImpl) -> Result { match (self, other) { - (TraitLikeImpl::NotImplemented, value) | (value, TraitLikeImpl::NotImplemented) => Ok(value), + (TraitLikeImpl::NotImplemented, value) | (value, TraitLikeImpl::NotImplemented) => { + Ok(value) + } (_, TraitLikeImpl::Implemented(span) | TraitLikeImpl::Custom(_, span)) => { Err(syn::Error::new(span, "conflicting type data registration")) } From 02eb8205bc2522051cdf21d085692418946093ed Mon Sep 17 00:00:00 2001 From: radiish Date: Sun, 22 Jan 2023 08:02:20 +0000 Subject: [PATCH 6/9] remove outdated test --- crates/bevy_reflect/src/lib.rs | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index cfad93e968cd6..6f4775dcd9d0d 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -313,36 +313,6 @@ mod tests { assert_eq!(Some(expected), my_struct); } - #[test] - fn from_reflect_should_use_default_container_attribute() { - #[derive(Reflect, FromReflect, Eq, PartialEq, Debug)] - #[reflect(Default)] - struct MyStruct { - foo: String, - #[reflect(ignore)] - bar: usize, - } - - impl Default for MyStruct { - fn default() -> Self { - Self { - foo: String::from("Hello"), - bar: 123, - } - } - } - - let expected = MyStruct { - foo: String::from("Hello"), - bar: 123, - }; - - let dyn_struct = DynamicStruct::default(); - let my_struct = ::from_reflect(&dyn_struct); - - assert_eq!(Some(expected), my_struct); - } - #[test] fn reflect_complex_patch() { #[derive(Reflect, Eq, PartialEq, Debug, FromReflect)] From b6b82cfc27d24e9f8fd399c9dd2bf59f38007c2a Mon Sep 17 00:00:00 2001 From: radiish Date: Sun, 22 Jan 2023 08:31:10 +0000 Subject: [PATCH 7/9] docs --- .../src/container_attributes.rs | 8 +++---- .../bevy_reflect_derive/src/lib.rs | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs index 0e9a5427a54a2..ee079c50d6161 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs @@ -15,13 +15,13 @@ use syn::spanned::Spanned; use syn::token::Comma; use syn::{parse_str, Lit, Meta, NestedMeta, Path}; -// The "special" trait-like idents that are used internally for reflection. +// The "special" trait-like attributes that are used internally for reflection. // Received via attributes like `#[reflect(partial_eq, hash, ...)]` const DEBUG_ATTR: &str = "debug"; const PARTIAL_EQ_ATTR: &str = "partial_eq"; const HASH_ATTR: &str = "hash"; -/// A marker for special trait-like ident implementations registered via the `Reflect` derive macro. +/// A marker for special trait-like attribute implementations registered via the `Reflect` derive macro. #[derive(Clone, Default)] pub(crate) enum TraitLikeImpl { /// The ident is not registered as implemented. @@ -56,11 +56,11 @@ impl TraitLikeImpl { /// A collection of traits that have been registered for a reflected type. /// /// This keeps track of a few traits that are utilized internally for reflection -/// (we'll call these traits _special trait-like idents_ within this context), but it +/// (we'll call these traits _special trait-like attributes_ within this context), but it /// will also keep track of all registered traits. Traits are registered as part of the /// `Reflect` derive macro using the helper attribute: `#[reflect(...)]`. /// -/// The list of special trait-like idents are as follows: +/// The list of special trait-like attributes are as follows: /// * `debug` /// * `hash` /// * `partial_eq` diff --git a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs index 6e1c54be18fad..2b181107bb6d2 100644 --- a/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs +++ b/crates/bevy_reflect/bevy_reflect_derive/src/lib.rs @@ -40,6 +40,29 @@ use syn::{parse_macro_input, DeriveInput}; pub(crate) static REFLECT_ATTRIBUTE_NAME: &str = "reflect"; pub(crate) static REFLECT_VALUE_ATTRIBUTE_NAME: &str = "reflect_value"; +/// Derives the `Reflect` trait. +/// +/// This macro supports the following container attributes (attributes on the item itself): +/// * `#[reflect(MyTrait)]`: Adds `ReflectMyTrait` into the `TypeRegistration` for this type. +/// * There are three "special" trait-like attributes: `#[reflect(debug, partial_eq, hash)]`. +/// These tell this macro to use [`Debug::fmt`], [`PartialEq::eq`] and [`Hash::hash`] respectivel +/// for its implementation of `reflect_debug`, `reflect_partial_eq` and `reflect_hash`. +/// * `#[reflect_value(...)]`: Supports all the same syntax as `#[reflect(...)]` +/// but tells this macro to treat this type as a basic value which cannot be broken down +/// into individual fields or variants. +/// * `reflect_value` and `reflect` are mutually exclusive. +/// +/// This macro also supports the following field attributes (on items without a `#[reflect_value]` attribute): +/// * `#[reflect(ignore)]`: Do not include this field in reflection +/// so it will never be read or written from a `dyn Reflect`. +/// The type of the field must implement [`Default`] for use in deserialization. +/// * `#[reflect(skip_serializing)]`: Include the field in reflection +/// so it can be read and written with a `dyn Reflect` but never (de)serialized. +/// The type of the field must implement [`Default`] for use in deserialization. +/// +/// [`Debug::fmt`]: core::fmt::Debug::fmt +/// [`PartialEq::eq`]: PartialEq::eq +/// [`Hash::hash`]: core::hash::Hash::hash #[proc_macro_derive(Reflect, attributes(reflect, reflect_value, module))] pub fn derive_reflect(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); From 353ee642c97acc0551fea1fc2292b46e8216034e Mon Sep 17 00:00:00 2001 From: radiish Date: Sun, 22 Jan 2023 08:35:28 +0000 Subject: [PATCH 8/9] remove unused import --- crates/bevy_reflect/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 6f4775dcd9d0d..4071f91c31997 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -107,8 +107,7 @@ mod tests { }; use std::any::TypeId; use std::fmt::{Debug, Formatter}; - - use super::prelude::*; + use super::*; use crate as bevy_reflect; use crate::serde::{ReflectSerializer, UntypedReflectDeserializer}; From c00651bcf4a3af152368a42043ccc72ddb16d86c Mon Sep 17 00:00:00 2001 From: radiish Date: Sun, 22 Jan 2023 14:37:06 +0000 Subject: [PATCH 9/9] `cargo fmt --all` --- crates/bevy_reflect/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_reflect/src/lib.rs b/crates/bevy_reflect/src/lib.rs index 4071f91c31997..476a341d3cfd5 100644 --- a/crates/bevy_reflect/src/lib.rs +++ b/crates/bevy_reflect/src/lib.rs @@ -107,7 +107,7 @@ mod tests { }; use std::any::TypeId; use std::fmt::{Debug, Formatter}; - + use super::*; use crate as bevy_reflect; use crate::serde::{ReflectSerializer, UntypedReflectDeserializer};