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
This commit is contained in:
stevehello166 2025-07-07 20:00:37 +00:00 committed by GitHub
parent 3b6d01972b
commit 6729208d39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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<Dir2> 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<Dir3> 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<Dir3A> 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<Dir4> 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;