From dd52291af497bf875a3c7c87d2c7fd81b5453b59 Mon Sep 17 00:00:00 2001 From: Joe Sorensen Date: Tue, 18 Jun 2024 15:03:23 -0600 Subject: [PATCH] Make `Debug` format of `Vec2/Pos2/Rot2` respect user precision (#4671) * closes #4665 pretty self explanatory, i opted for 3 instead of 2 since i think that's what display does --- crates/emath/src/pos2.rs | 6 +++++- crates/emath/src/rot2.rs | 22 ++++++++++++++++------ crates/emath/src/vec2.rs | 6 +++++- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/crates/emath/src/pos2.rs b/crates/emath/src/pos2.rs index 6e0ef212b..2f3d1bd1b 100644 --- a/crates/emath/src/pos2.rs +++ b/crates/emath/src/pos2.rs @@ -319,7 +319,11 @@ impl Div for Pos2 { impl fmt::Debug for Pos2 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "[{:.1} {:.1}]", self.x, self.y) + if let Some(precision) = f.precision() { + write!(f, "[{1:.0$} {2:.0$}]", precision, self.x, self.y) + } else { + write!(f, "[{:.1} {:.1}]", self.x, self.y) + } } } diff --git a/crates/emath/src/rot2.rs b/crates/emath/src/rot2.rs index f9e0ae646..45d1f0b55 100644 --- a/crates/emath/src/rot2.rs +++ b/crates/emath/src/rot2.rs @@ -91,12 +91,22 @@ impl Rot2 { impl std::fmt::Debug for Rot2 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "Rot2 {{ angle: {:.1}°, length: {} }}", - self.angle().to_degrees(), - self.length() - ) + if let Some(precision) = f.precision() { + write!( + f, + "Rot2 {{ angle: {:.2$}°, length: {} }}", + self.angle().to_degrees(), + self.length(), + precision + ) + } else { + write!( + f, + "Rot2 {{ angle: {:.1}°, length: {} }}", + self.angle().to_degrees(), + self.length(), + ) + } } } diff --git a/crates/emath/src/vec2.rs b/crates/emath/src/vec2.rs index 29050935f..483de197d 100644 --- a/crates/emath/src/vec2.rs +++ b/crates/emath/src/vec2.rs @@ -468,7 +468,11 @@ impl Div for Vec2 { impl fmt::Debug for Vec2 { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "[{:.1} {:.1}]", self.x, self.y) + if let Some(precision) = f.precision() { + write!(f, "[{1:.0$} {2:.0$}]", precision, self.x, self.y) + } else { + write!(f, "[{:.1} {:.1}]", self.x, self.y) + } } }