Skip to content

Commit faeccd7

Browse files
committed
Reflection cleanup (#1536)
This is an effort to provide the correct `#[reflect_value(...)]` attributes where they are needed. Supersedes #1533 and resolves #1528. --- I am working under the following assumptions (thanks to @bjorn3 and @Davier for advice here): - Any `enum` that derives `Reflect` and one or more of { `Serialize`, `Deserialize`, `PartialEq`, `Hash` } needs a `#[reflect_value(...)]` attribute containing the same subset of { `Serialize`, `Deserialize`, `PartialEq`, `Hash` } that is present on the derive. - Same as above for `struct` and `#[reflect(...)]`, respectively. - If a `struct` is used as a component, it should also have `#[reflect(Component)]` - All reflected types should be registered in their plugins I treated the following as components (added `#[reflect(Component)]` if necessary): - `bevy_render` - `struct RenderLayers` - `bevy_transform` - `struct GlobalTransform` - `struct Parent` - `struct Transform` - `bevy_ui` - `struct Style` Not treated as components: - `bevy_math` - `struct Size<T>` - `struct Rect<T>` - Note: The updates for `Size<T>` and `Rect<T>` in `bevy::math::geometry` required using @Davier's suggestion to add `+ PartialEq` to the trait bound. I then registered the specific types used over in `bevy_ui` such as `Size<Val>`, etc. in `bevy_ui`'s plugin, since `bevy::math` does not contain a plugin. - `bevy_render` - `struct Color` - `struct PipelineSpecialization` - `struct ShaderSpecialization` - `enum PrimitiveTopology` - `enum IndexFormat` Not Addressed: - I am not searching for components in Bevy that are _not_ reflected. So if there are components that are not reflected that should be reflected, that will need to be figured out in another PR. - I only added `#[reflect(...)]` or `#[reflect_value(...)]` entries for the set of four traits { `Serialize`, `Deserialize`, `PartialEq`, `Hash` } _if they were derived via `#[derive(...)]`_. I did not look for manual trait implementations of the same set of four, nor did I consider any traits outside the four. Are those other possibilities something that needs to be looked into?
1 parent 5147232 commit faeccd7

File tree

15 files changed

+91
-26
lines changed

15 files changed

+91
-26
lines changed

crates/bevy_core/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod prelude {
1818

1919
use bevy_app::prelude::*;
2020
use bevy_ecs::{entity::Entity, system::IntoSystem};
21+
use bevy_utils::HashSet;
2122
use std::ops::Range;
2223

2324
/// Adds core functionality to Apps.
@@ -36,6 +37,8 @@ impl Plugin for CorePlugin {
3637
app.init_resource::<Time>()
3738
.init_resource::<EntityLabels>()
3839
.init_resource::<FixedTimesteps>()
40+
.register_type::<HashSet<String>>()
41+
.register_type::<Option<String>>()
3942
.register_type::<Entity>()
4043
.register_type::<Name>()
4144
.register_type::<Labels>()

crates/bevy_math/src/geometry.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
44

55
/// A two dimensional "size" as defined by a width and height
66
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
7-
pub struct Size<T: Reflect = f32> {
7+
#[reflect(PartialEq)]
8+
pub struct Size<T: Reflect + PartialEq = f32> {
89
pub width: T,
910
pub height: T,
1011
}
1112

12-
impl<T: Reflect> Size<T> {
13+
impl<T: Reflect + PartialEq> Size<T> {
1314
pub fn new(width: T, height: T) -> Self {
1415
Size { width, height }
1516
}
1617
}
1718

18-
impl<T: Default + Reflect> Default for Size<T> {
19+
impl<T: Default + Reflect + PartialEq> Default for Size<T> {
1920
fn default() -> Self {
2021
Self {
2122
width: Default::default(),
@@ -26,14 +27,15 @@ impl<T: Default + Reflect> Default for Size<T> {
2627

2728
/// A rect, as defined by its "side" locations
2829
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
29-
pub struct Rect<T: Reflect> {
30+
#[reflect(PartialEq)]
31+
pub struct Rect<T: Reflect + PartialEq> {
3032
pub left: T,
3133
pub right: T,
3234
pub top: T,
3335
pub bottom: T,
3436
}
3537

36-
impl<T: Reflect> Rect<T> {
38+
impl<T: Reflect + PartialEq> Rect<T> {
3739
pub fn all(value: T) -> Self
3840
where
3941
T: Clone,
@@ -47,7 +49,7 @@ impl<T: Reflect> Rect<T> {
4749
}
4850
}
4951

50-
impl<T: Default + Reflect> Default for Rect<T> {
52+
impl<T: Default + Reflect + PartialEq> Default for Rect<T> {
5153
fn default() -> Self {
5254
Self {
5355
left: Default::default(),
@@ -58,7 +60,7 @@ impl<T: Default + Reflect> Default for Rect<T> {
5860
}
5961
}
6062

61-
impl<T: Reflect> Add<Vec2> for Size<T>
63+
impl<T: Reflect + PartialEq> Add<Vec2> for Size<T>
6264
where
6365
T: Add<f32, Output = T>,
6466
{
@@ -72,7 +74,7 @@ where
7274
}
7375
}
7476

75-
impl<T: Reflect> AddAssign<Vec2> for Size<T>
77+
impl<T: Reflect + PartialEq> AddAssign<Vec2> for Size<T>
7678
where
7779
T: AddAssign<f32>,
7880
{
@@ -82,7 +84,7 @@ where
8284
}
8385
}
8486

85-
impl<T: Reflect> Sub<Vec2> for Size<T>
87+
impl<T: Reflect + PartialEq> Sub<Vec2> for Size<T>
8688
where
8789
T: Sub<f32, Output = T>,
8890
{
@@ -96,7 +98,7 @@ where
9698
}
9799
}
98100

99-
impl<T: Reflect> SubAssign<Vec2> for Size<T>
101+
impl<T: Reflect + PartialEq> SubAssign<Vec2> for Size<T>
100102
where
101103
T: SubAssign<f32>,
102104
{
@@ -106,7 +108,7 @@ where
106108
}
107109
}
108110

109-
impl<T: Reflect> Mul<f32> for Size<T>
111+
impl<T: Reflect + PartialEq> Mul<f32> for Size<T>
110112
where
111113
T: Mul<f32, Output = T>,
112114
{
@@ -120,7 +122,7 @@ where
120122
}
121123
}
122124

123-
impl<T: Reflect> MulAssign<f32> for Size<T>
125+
impl<T: Reflect + PartialEq> MulAssign<f32> for Size<T>
124126
where
125127
T: MulAssign<f32>,
126128
{
@@ -130,7 +132,7 @@ where
130132
}
131133
}
132134

133-
impl<T: Reflect> Div<f32> for Size<T>
135+
impl<T: Reflect + PartialEq> Div<f32> for Size<T>
134136
where
135137
T: Div<f32, Output = T>,
136138
{
@@ -144,7 +146,7 @@ where
144146
}
145147
}
146148

147-
impl<T: Reflect> DivAssign<f32> for Size<T>
149+
impl<T: Reflect + PartialEq> DivAssign<f32> for Size<T>
148150
where
149151
T: DivAssign<f32>,
150152
{

crates/bevy_reflect/src/impls/std.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
map_partial_eq, serde::Serializable, DynamicMap, List, ListIter, Map, MapIter, Reflect,
3-
ReflectDeserialize, ReflectMut, ReflectRef,
2+
map_partial_eq, serde::Serializable, DynamicMap, FromType, GetTypeRegistration, List, ListIter,
3+
Map, MapIter, Reflect, ReflectDeserialize, ReflectMut, ReflectRef, TypeRegistration,
44
};
55

66
use bevy_reflect_derive::impl_reflect_value;
@@ -112,6 +112,14 @@ impl<T: Reflect> Reflect for Vec<T> {
112112
}
113113
}
114114

115+
impl<T: Reflect + for<'de> Deserialize<'de>> GetTypeRegistration for Vec<T> {
116+
fn get_type_registration() -> TypeRegistration {
117+
let mut registration = TypeRegistration::of::<Vec<T>>();
118+
registration.insert::<ReflectDeserialize>(FromType::<Vec<T>>::from_type());
119+
registration
120+
}
121+
}
122+
115123
impl<K: Reflect + Clone + Eq + Hash, V: Reflect + Clone> Map for HashMap<K, V> {
116124
fn get(&self, key: &dyn Reflect) -> Option<&dyn Reflect> {
117125
key.downcast_ref::<K>()
@@ -207,6 +215,18 @@ impl<K: Reflect + Clone + Eq + Hash, V: Reflect + Clone> Reflect for HashMap<K,
207215
}
208216
}
209217

218+
impl<K, V> GetTypeRegistration for HashMap<K, V>
219+
where
220+
K: Reflect + Clone + Eq + Hash + for<'de> Deserialize<'de>,
221+
V: Reflect + Clone + for<'de> Deserialize<'de>,
222+
{
223+
fn get_type_registration() -> TypeRegistration {
224+
let mut registration = TypeRegistration::of::<HashMap<K, V>>();
225+
registration.insert::<ReflectDeserialize>(FromType::<HashMap<K, V>>::from_type());
226+
registration
227+
}
228+
}
229+
210230
impl Reflect for Cow<'static, str> {
211231
fn type_name(&self) -> &str {
212232
std::any::type_name::<Self>()
@@ -266,3 +286,11 @@ impl Reflect for Cow<'static, str> {
266286
Some(Serializable::Borrowed(self))
267287
}
268288
}
289+
290+
impl GetTypeRegistration for Cow<'static, str> {
291+
fn get_type_registration() -> TypeRegistration {
292+
let mut registration = TypeRegistration::of::<Cow<'static, str>>();
293+
registration.insert::<ReflectDeserialize>(FromType::<Cow<'static, str>>::from_type());
294+
registration
295+
}
296+
}

crates/bevy_reflect/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ mod tests {
194194
#[test]
195195
fn reflect_complex_patch() {
196196
#[derive(Reflect, Eq, PartialEq, Debug)]
197+
#[reflect(PartialEq)]
197198
struct Foo {
198199
a: u32,
199200
#[reflect(ignore)]
@@ -205,6 +206,7 @@ mod tests {
205206
}
206207

207208
#[derive(Reflect, Eq, PartialEq, Debug)]
209+
#[reflect(PartialEq)]
208210
struct Bar {
209211
x: u32,
210212
}
@@ -317,6 +319,7 @@ mod tests {
317319
#[test]
318320
fn reflect_take() {
319321
#[derive(Reflect, Debug, PartialEq)]
322+
#[reflect(PartialEq)]
320323
struct Bar {
321324
x: u32,
322325
}

crates/bevy_render/src/camera/visible_entities.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub type Layer = u8;
4343
///
4444
/// Entities without this component belong to layer `0`.
4545
#[derive(Copy, Clone, Reflect, PartialEq, Eq, PartialOrd, Ord)]
46-
#[reflect(Component)]
46+
#[reflect(Component, PartialEq)]
4747
pub struct RenderLayers(LayerMask);
4848

4949
impl std::fmt::Debug for RenderLayers {

crates/bevy_render/src/color.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
use bevy_asset::Handle;
88
use bevy_core::{Byteable, Bytes};
99
use bevy_math::{Vec3, Vec4};
10-
use bevy_reflect::Reflect;
10+
use bevy_reflect::{Reflect, ReflectDeserialize};
1111
use serde::{Deserialize, Serialize};
1212
use std::ops::{Add, AddAssign, Mul, MulAssign};
1313

@@ -16,6 +16,7 @@ use std::ops::{Add, AddAssign, Mul, MulAssign};
1616
/// RGBA color in the Linear sRGB colorspace (often colloquially referred to as "linear", "RGB", or "linear RGB").
1717
#[repr(C)]
1818
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Reflect)]
19+
#[reflect(PartialEq, Serialize, Deserialize)]
1920
pub struct Color {
2021
red: f32,
2122
green: f32,

crates/bevy_render/src/lib.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ use bevy_app::prelude::*;
3939
use bevy_asset::{AddAsset, AssetStage};
4040
use bevy_ecs::schedule::StageLabel;
4141
use camera::{
42-
ActiveCameras, Camera, OrthographicProjection, PerspectiveProjection, VisibleEntities,
42+
ActiveCameras, Camera, DepthCalculation, OrthographicProjection, PerspectiveProjection,
43+
RenderLayers, ScalingMode, VisibleEntities, WindowOrigin,
4344
};
4445
use pipeline::{
4546
IndexFormat, PipelineCompiler, PipelineDescriptor, PipelineSpecialization, PrimitiveTopology,
46-
ShaderSpecialization,
47+
ShaderSpecialization, VertexBufferLayout,
4748
};
4849
use render_graph::{
4950
base::{self, BaseRenderGraphConfig, MainPass},
@@ -125,6 +126,7 @@ impl Plugin for RenderPlugin {
125126
.add_asset::<Shader>()
126127
.add_asset::<PipelineDescriptor>()
127128
.register_type::<Camera>()
129+
.register_type::<DepthCalculation>()
128130
.register_type::<Draw>()
129131
.register_type::<Visible>()
130132
.register_type::<RenderPipelines>()
@@ -137,6 +139,10 @@ impl Plugin for RenderPlugin {
137139
.register_type::<PrimitiveTopology>()
138140
.register_type::<IndexFormat>()
139141
.register_type::<PipelineSpecialization>()
142+
.register_type::<RenderLayers>()
143+
.register_type::<ScalingMode>()
144+
.register_type::<VertexBufferLayout>()
145+
.register_type::<WindowOrigin>()
140146
.init_resource::<ClearColor>()
141147
.init_resource::<RenderGraph>()
142148
.init_resource::<PipelineCompiler>()

crates/bevy_render/src/pipeline/pipeline_compiler.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use crate::{
55
shader::{Shader, ShaderError},
66
};
77
use bevy_asset::{Assets, Handle};
8-
use bevy_reflect::Reflect;
8+
use bevy_reflect::{Reflect, ReflectDeserialize};
99
use bevy_utils::{HashMap, HashSet};
1010
use once_cell::sync::Lazy;
1111
use serde::{Deserialize, Serialize};
1212

1313
#[derive(Clone, Eq, PartialEq, Debug, Reflect)]
14+
#[reflect(PartialEq)]
1415
pub struct PipelineSpecialization {
1516
pub shader_specialization: ShaderSpecialization,
1617
pub primitive_topology: PrimitiveTopology,
@@ -41,6 +42,7 @@ impl PipelineSpecialization {
4142
}
4243

4344
#[derive(Clone, Eq, PartialEq, Debug, Default, Reflect, Serialize, Deserialize)]
45+
#[reflect(PartialEq, Serialize, Deserialize)]
4446
pub struct ShaderSpecialization {
4547
pub shader_defs: HashSet<String>,
4648
}

crates/bevy_render/src/pipeline/state_descriptors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::texture::TextureFormat;
2-
use bevy_reflect::Reflect;
2+
use bevy_reflect::{Reflect, ReflectDeserialize};
33
use serde::{Deserialize, Serialize};
44

55
#[derive(Clone, Debug)]
@@ -89,6 +89,7 @@ pub enum CompareFunction {
8989

9090
/// Describes how the VertexAttributes should be interpreted while rendering
9191
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize, Reflect)]
92+
#[reflect_value(Serialize, Deserialize, PartialEq, Hash)]
9293
pub enum PrimitiveTopology {
9394
PointList = 0,
9495
LineList = 1,
@@ -227,6 +228,7 @@ impl Default for BlendOperation {
227228
}
228229

229230
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize, Reflect)]
231+
#[reflect_value(Hash, PartialEq, Serialize, Deserialize)]
230232
pub enum IndexFormat {
231233
Uint16 = 0,
232234
Uint32 = 1,

crates/bevy_sprite/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl Plugin for SpritePlugin {
4848
app.add_asset::<ColorMaterial>()
4949
.add_asset::<TextureAtlas>()
5050
.register_type::<Sprite>()
51+
.register_type::<SpriteResizeMode>()
5152
.add_system_to_stage(CoreStage::PostUpdate, sprite_system.system())
5253
.add_system_to_stage(
5354
CoreStage::PostUpdate,

crates/bevy_transform/src/components/global_transform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy_reflect::Reflect;
55
use std::ops::Mul;
66

77
#[derive(Debug, PartialEq, Clone, Copy, Reflect)]
8-
#[reflect(Component)]
8+
#[reflect(Component, PartialEq)]
99
pub struct GlobalTransform {
1010
pub translation: Vec3,
1111
pub rotation: Quat,

crates/bevy_transform/src/components/parent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bevy_reflect::Reflect;
77
use std::ops::{Deref, DerefMut};
88

99
#[derive(Debug, Copy, Clone, Eq, PartialEq, Reflect)]
10-
#[reflect(Component, MapEntities)]
10+
#[reflect(Component, MapEntities, PartialEq)]
1111
pub struct Parent(pub Entity);
1212

1313
// TODO: We need to impl either FromWorld or Default so Parent can be registered as Properties.
@@ -42,7 +42,7 @@ impl DerefMut for Parent {
4242
}
4343

4444
#[derive(Debug, Copy, Clone, Eq, PartialEq, Reflect)]
45-
#[reflect(Component, MapEntities)]
45+
#[reflect(Component, MapEntities, PartialEq)]
4646
pub struct PreviousParent(pub(crate) Entity);
4747

4848
impl MapEntities for PreviousParent {

crates/bevy_transform/src/components/transform.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use bevy_reflect::Reflect;
55
use std::ops::Mul;
66

77
#[derive(Debug, PartialEq, Clone, Copy, Reflect)]
8-
#[reflect(Component)]
8+
#[reflect(Component, PartialEq)]
99
pub struct Transform {
1010
pub translation: Vec3,
1111
pub rotation: Quat,

crates/bevy_ui/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod update;
99
pub mod widget;
1010

1111
pub use anchors::*;
12+
use bevy_math::{Rect, Size};
1213
use bevy_render::RenderStage;
1314
pub use flex::*;
1415
pub use focus::*;
@@ -47,6 +48,21 @@ pub mod system {
4748
impl Plugin for UiPlugin {
4849
fn build(&self, app: &mut AppBuilder) {
4950
app.init_resource::<FlexSurface>()
51+
.register_type::<AlignContent>()
52+
.register_type::<AlignItems>()
53+
.register_type::<AlignSelf>()
54+
.register_type::<Direction>()
55+
.register_type::<Display>()
56+
.register_type::<FlexDirection>()
57+
.register_type::<FlexWrap>()
58+
.register_type::<JustifyContent>()
59+
.register_type::<Node>()
60+
.register_type::<PositionType>()
61+
.register_type::<Size<f32>>()
62+
.register_type::<Size<Val>>()
63+
.register_type::<Rect<Val>>()
64+
.register_type::<Style>()
65+
.register_type::<Val>()
5066
.add_stage_before(CoreStage::PostUpdate, UiStage::Ui, SystemStage::parallel())
5167
.add_system_to_stage(CoreStage::PreUpdate, ui_focus_system.system())
5268
// add these stages to front because these must run before transform update systems

0 commit comments

Comments
 (0)