Skip to content

Commit 6729208

Browse files
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
1 parent 3b6d019 commit 6729208

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

crates/bevy_math/src/direction.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
};
55

66
use core::f32::consts::FRAC_1_SQRT_2;
7+
use core::fmt;
78
use derive_more::derive::Into;
89

910
#[cfg(feature = "bevy_reflect")]
@@ -325,6 +326,12 @@ impl core::ops::Mul<Dir2> for Rot2 {
325326
}
326327
}
327328

329+
impl fmt::Display for Dir2 {
330+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331+
write!(f, "{}", self.0)
332+
}
333+
}
334+
328335
#[cfg(any(feature = "approx", test))]
329336
impl approx::AbsDiffEq for Dir2 {
330337
type Epsilon = f32;
@@ -587,6 +594,12 @@ impl core::ops::Mul<Dir3> for Quat {
587594
}
588595
}
589596

597+
impl fmt::Display for Dir3 {
598+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
599+
write!(f, "{}", self.0)
600+
}
601+
}
602+
590603
#[cfg(feature = "approx")]
591604
impl approx::AbsDiffEq for Dir3 {
592605
type Epsilon = f32;
@@ -834,6 +847,12 @@ impl core::ops::Mul<Dir3A> for Quat {
834847
}
835848
}
836849

850+
impl fmt::Display for Dir3A {
851+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
852+
write!(f, "{}", self.0)
853+
}
854+
}
855+
837856
#[cfg(feature = "approx")]
838857
impl approx::AbsDiffEq for Dir3A {
839858
type Epsilon = f32;
@@ -1022,6 +1041,12 @@ impl core::ops::Mul<Dir4> for f32 {
10221041
}
10231042
}
10241043

1044+
impl fmt::Display for Dir4 {
1045+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1046+
write!(f, "{}", self.0)
1047+
}
1048+
}
1049+
10251050
#[cfg(feature = "approx")]
10261051
impl approx::AbsDiffEq for Dir4 {
10271052
type Epsilon = f32;

0 commit comments

Comments
 (0)