From bc2ae243463590cf7f77d02f657d4a1afe06393b Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Sun, 28 Apr 2024 01:19:48 -0500 Subject: [PATCH] Add a `Display` impl for `Vec2`, `Pos2`, and `Rect` These three types currently have a `Debug` implementation that only ever prints one decimal point. Sometimes it is useful to see more of the number, or otherwise have specific formatting. Add `Display` implementations that pass the format specification to the member `f32`s for an easier way to control what is shown when debugging. --- crates/emath/src/pos2.rs | 16 ++++++++++++++-- crates/emath/src/rect.rs | 16 ++++++++++++++-- crates/emath/src/vec2.rs | 16 ++++++++++++++-- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/crates/emath/src/pos2.rs b/crates/emath/src/pos2.rs index f65efaef43d..6e0ef212ba1 100644 --- a/crates/emath/src/pos2.rs +++ b/crates/emath/src/pos2.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::ops::{Add, AddAssign, Sub, SubAssign}; use crate::*; @@ -316,8 +317,19 @@ impl Div for Pos2 { } } -impl std::fmt::Debug for Pos2 { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl fmt::Debug for Pos2 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "[{:.1} {:.1}]", self.x, self.y) } } + +impl fmt::Display for Pos2 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("[")?; + self.x.fmt(f)?; + f.write_str(" ")?; + self.y.fmt(f)?; + f.write_str("]")?; + Ok(()) + } +} diff --git a/crates/emath/src/rect.rs b/crates/emath/src/rect.rs index f23d3500520..c1a7f603ecb 100644 --- a/crates/emath/src/rect.rs +++ b/crates/emath/src/rect.rs @@ -1,4 +1,5 @@ use std::f32::INFINITY; +use std::fmt; use crate::*; @@ -631,12 +632,23 @@ impl Rect { } } -impl std::fmt::Debug for Rect { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl fmt::Debug for Rect { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "[{:?} - {:?}]", self.min, self.max) } } +impl fmt::Display for Rect { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("[")?; + self.min.fmt(f)?; + f.write_str(" - ")?; + self.max.fmt(f)?; + f.write_str("]")?; + Ok(()) + } +} + /// from (min, max) or (left top, right bottom) impl From<[Pos2; 2]> for Rect { #[inline] diff --git a/crates/emath/src/vec2.rs b/crates/emath/src/vec2.rs index 0cab00fe62f..99f7d0c05c7 100644 --- a/crates/emath/src/vec2.rs +++ b/crates/emath/src/vec2.rs @@ -1,3 +1,4 @@ +use std::fmt; use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use crate::Vec2b; @@ -464,12 +465,23 @@ impl Div for Vec2 { } } -impl std::fmt::Debug for Vec2 { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl fmt::Debug for Vec2 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "[{:.1} {:.1}]", self.x, self.y) } } +impl fmt::Display for Vec2 { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("[")?; + self.x.fmt(f)?; + f.write_str(" ")?; + self.y.fmt(f)?; + f.write_str("]")?; + Ok(()) + } +} + #[test] fn test_vec2() { macro_rules! almost_eq {