From 1a41c736b39a01f4dbef75c4282edf3ced5f01e9 Mon Sep 17 00:00:00 2001 From: Gino Valente <49806985+MrGVSV@users.noreply.github.com> Date: Mon, 23 Sep 2024 15:50:12 -0700 Subject: [PATCH] bevy_reflect: Update `EulerRot` to match `glam` 0.29 (#15402) # Objective #15349 added an `impl_reflect!` for `glam::EulerRot`. This was done by copying and pasting the enum definition from `glam` into `bevy_reflect` so that the macro could interpret the variants. However, as mentioned in the description for that PR, this would need to be updated for `glam` 0.29, as it had not been updated yet. #15249 came and updated `glam` to 0.29, but did not change these impls. This is understandable as failing to do so doesn't cause any compile errors. This PR updates the definition and aims to make this silent breakage a little less silent. ## Solution Firstly, I updated the definition for `EulerRot` to match the one from `glam`. Secondly, I added the `assert_type_match` crate, which I created specifically to solve this problem. By using this crate, we'll get a compile time error if `glam` ever decides to change `EulerRot` again. In the future we can consider using it for other types with this problem, including in other crates (I'm pretty sure `bevy_window` and/or `bevy_winit` also copy+paste some types). I made sure to use as few dependencies as possible so everything should already be in-tree (it's just `quote`, `proc-macro2`, and `syn` with default features). ## Testing No tests added. CI should pass. --- ## Migration Guide The reflection implementation for `EulerRot` has been updated to align with `glam` 0.29. Please update any reflection-based usages accordingly. --- crates/bevy_reflect/Cargo.toml | 1 + crates/bevy_reflect/src/impls/glam.rs | 32 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/crates/bevy_reflect/Cargo.toml b/crates/bevy_reflect/Cargo.toml index 328fb47ebc..1ce6ebc07b 100644 --- a/crates/bevy_reflect/Cargo.toml +++ b/crates/bevy_reflect/Cargo.toml @@ -41,6 +41,7 @@ downcast-rs = "1.2" thiserror = "1.0" serde = "1" smallvec = { version = "1.11", optional = true } +assert_type_match = "0.1.1" glam = { version = "0.29", features = ["serde"], optional = true } petgraph = { version = "0.6", features = ["serde-1"], optional = true } diff --git a/crates/bevy_reflect/src/impls/glam.rs b/crates/bevy_reflect/src/impls/glam.rs index 11f13692de..0b3b81dc80 100644 --- a/crates/bevy_reflect/src/impls/glam.rs +++ b/crates/bevy_reflect/src/impls/glam.rs @@ -1,8 +1,20 @@ use crate as bevy_reflect; use crate::{std_traits::ReflectDefault, ReflectDeserialize, ReflectSerialize}; +use assert_type_match::assert_type_match; use bevy_reflect_derive::{impl_reflect, impl_reflect_opaque}; use glam::*; +/// Reflects the given foreign type as an enum and asserts that the variants/fields match up. +macro_rules! reflect_enum { + ($(#[$meta:meta])* enum $ident:ident { $($ty:tt)* } ) => { + impl_reflect!($(#[$meta])* enum $ident { $($ty)* }); + + #[assert_type_match($ident, test_only)] + #[allow(clippy::upper_case_acronyms)] + enum $ident { $($ty)* } + }; +} + impl_reflect!( #[reflect(Debug, Hash, PartialEq, Default, Deserialize, Serialize)] #[type_path = "glam"] @@ -330,7 +342,7 @@ impl_reflect!( } ); -impl_reflect!( +reflect_enum!( #[reflect(Debug, PartialEq, Default, Deserialize, Serialize)] #[type_path = "glam"] enum EulerRot { @@ -340,6 +352,24 @@ impl_reflect!( YZX, XYZ, XZY, + ZYZ, + ZXZ, + YXY, + YZY, + XYX, + XZX, + ZYXEx, + ZXYEx, + YXZEx, + YZXEx, + XYZEx, + XZYEx, + ZYZEx, + ZXZEx, + YXYEx, + YZYEx, + XYXEx, + XZXEx, } );