From 6729208d397ca8140fd130165f0d52bc0d0ea2d5 Mon Sep 17 00:00:00 2001 From: stevehello166 <77701792+stevehello166@users.noreply.github.com> Date: Mon, 7 Jul 2025 20:00:37 +0000 Subject: [PATCH] Implement display for direction (#19942) # Objective To implement fmt::Display for the direction types. The reason that this would be a good addition is that I often find myself using println! to debug things with directions and adding the extra ":?" was getting a little annoying. It would also be better for any potential CLI apps that might need to output a direction. ## Solution Copied glam's implementation of Display for each length of direction. I.E Vec3's display for Dir3. ## Testing - Did you test these changes? If so, how? Yes, I wrote a little script that printed out the different directions and compared it to their vector counterparts. Here it is if anyone's interested ``` use bevy_math::*; fn main() { let dir2 = Dir2::from_xy(0.0, 1.0).unwrap(); let dir3 = Dir3::from_xyz(0.0, 1.0, 0.0).unwrap(); let dir3a = Dir3A::from_xyz(0.0, 1.0, 0.0).unwrap(); let dir4 = Dir4::from_xyzw(0.0, 1.0, 0.0, 0.0).unwrap(); let vec2 = Vec2::new(0.0, 1.0); let vec3 = Vec3::new(0.0, 1.0, 0.0); let vec4 = Vec4::new(0.0, 1.0, 0.0, 1.0); println!("{dir2} {dir3} {dir3a} {dir4}"); println!("{vec2}, {vec3}, {vec4}") } ``` - Are there any parts that need more testing? Perhaps --- crates/bevy_math/src/direction.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/crates/bevy_math/src/direction.rs b/crates/bevy_math/src/direction.rs index f5ecf75c08..03cb9f969f 100644 --- a/crates/bevy_math/src/direction.rs +++ b/crates/bevy_math/src/direction.rs @@ -4,6 +4,7 @@ use crate::{ }; use core::f32::consts::FRAC_1_SQRT_2; +use core::fmt; use derive_more::derive::Into; #[cfg(feature = "bevy_reflect")] @@ -325,6 +326,12 @@ impl core::ops::Mul for Rot2 { } } +impl fmt::Display for Dir2 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + #[cfg(any(feature = "approx", test))] impl approx::AbsDiffEq for Dir2 { type Epsilon = f32; @@ -587,6 +594,12 @@ impl core::ops::Mul for Quat { } } +impl fmt::Display for Dir3 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + #[cfg(feature = "approx")] impl approx::AbsDiffEq for Dir3 { type Epsilon = f32; @@ -834,6 +847,12 @@ impl core::ops::Mul for Quat { } } +impl fmt::Display for Dir3A { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + #[cfg(feature = "approx")] impl approx::AbsDiffEq for Dir3A { type Epsilon = f32; @@ -1022,6 +1041,12 @@ impl core::ops::Mul for f32 { } } +impl fmt::Display for Dir4 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} + #[cfg(feature = "approx")] impl approx::AbsDiffEq for Dir4 { type Epsilon = f32;