Skip to content

Commit 2fabde1

Browse files
authored
Add a Display impl for Vec2, Pos2, and Rect (#4428)
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. This allows doing e.g. `ui.label(format!("{:.4}", rect * scale))` which currently prints zeroes if scale is small.
1 parent af39bd2 commit 2fabde1

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

crates/emath/src/pos2.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::ops::{Add, AddAssign, Sub, SubAssign};
23

34
use crate::*;
@@ -316,8 +317,19 @@ impl Div<f32> for Pos2 {
316317
}
317318
}
318319

319-
impl std::fmt::Debug for Pos2 {
320-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
320+
impl fmt::Debug for Pos2 {
321+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321322
write!(f, "[{:.1} {:.1}]", self.x, self.y)
322323
}
323324
}
325+
326+
impl fmt::Display for Pos2 {
327+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328+
f.write_str("[")?;
329+
self.x.fmt(f)?;
330+
f.write_str(" ")?;
331+
self.y.fmt(f)?;
332+
f.write_str("]")?;
333+
Ok(())
334+
}
335+
}

crates/emath/src/rect.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::f32::INFINITY;
2+
use std::fmt;
23

34
use crate::*;
45

@@ -631,12 +632,23 @@ impl Rect {
631632
}
632633
}
633634

634-
impl std::fmt::Debug for Rect {
635-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
635+
impl fmt::Debug for Rect {
636+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
636637
write!(f, "[{:?} - {:?}]", self.min, self.max)
637638
}
638639
}
639640

641+
impl fmt::Display for Rect {
642+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
643+
f.write_str("[")?;
644+
self.min.fmt(f)?;
645+
f.write_str(" - ")?;
646+
self.max.fmt(f)?;
647+
f.write_str("]")?;
648+
Ok(())
649+
}
650+
}
651+
640652
/// from (min, max) or (left top, right bottom)
641653
impl From<[Pos2; 2]> for Rect {
642654
#[inline]

crates/emath/src/vec2.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt;
12
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
23

34
use crate::Vec2b;
@@ -464,12 +465,23 @@ impl Div<f32> for Vec2 {
464465
}
465466
}
466467

467-
impl std::fmt::Debug for Vec2 {
468-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
468+
impl fmt::Debug for Vec2 {
469+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
469470
write!(f, "[{:.1} {:.1}]", self.x, self.y)
470471
}
471472
}
472473

474+
impl fmt::Display for Vec2 {
475+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
476+
f.write_str("[")?;
477+
self.x.fmt(f)?;
478+
f.write_str(" ")?;
479+
self.y.fmt(f)?;
480+
f.write_str("]")?;
481+
Ok(())
482+
}
483+
}
484+
473485
#[test]
474486
fn test_vec2() {
475487
macro_rules! almost_eq {

0 commit comments

Comments
 (0)