Browse Source
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.
pull/1897/head
Trevor Gross
6 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with
42 additions and
6 deletions
crates/emath/src/pos2.rs
crates/emath/src/rect.rs
crates/emath/src/vec2.rs
@ -1,3 +1,4 @@
use std ::fmt ;
use std ::ops ::{ Add , AddAssign , Sub , SubAssign } ;
use std ::ops ::{ Add , AddAssign , Sub , SubAssign } ;
use crate ::* ;
use crate ::* ;
@ -316,8 +317,19 @@ impl Div<f32> for Pos2 {
}
}
}
}
impl std ::fmt ::Debug for Pos2 {
impl fmt ::Debug for Pos2 {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
write ! ( f , "[{:.1} {:.1}]" , self . x , self . y )
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 ( ( ) )
}
}
@ -1,4 +1,5 @@
use std ::f32 ::INFINITY ;
use std ::f32 ::INFINITY ;
use std ::fmt ;
use crate ::* ;
use crate ::* ;
@ -631,12 +632,23 @@ impl Rect {
}
}
}
}
impl std ::fmt ::Debug for Rect {
impl fmt ::Debug for Rect {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
write ! ( f , "[{:?} - {:?}]" , self . min , self . max )
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)
/// from (min, max) or (left top, right bottom)
impl From < [ Pos2 ; 2 ] > for Rect {
impl From < [ Pos2 ; 2 ] > for Rect {
#[ inline ]
#[ inline ]
@ -1,3 +1,4 @@
use std ::fmt ;
use std ::ops ::{ Add , AddAssign , Div , DivAssign , Mul , MulAssign , Neg , Sub , SubAssign } ;
use std ::ops ::{ Add , AddAssign , Div , DivAssign , Mul , MulAssign , Neg , Sub , SubAssign } ;
use crate ::Vec2b ;
use crate ::Vec2b ;
@ -464,12 +465,23 @@ impl Div<f32> for Vec2 {
}
}
}
}
impl std ::fmt ::Debug for Vec2 {
impl fmt ::Debug for Vec2 {
fn fmt ( & self , f : & mut std ::fmt ::Formatter < '_ > ) -> std ::fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < '_ > ) -> fmt ::Result {
write ! ( f , "[{:.1} {:.1}]" , self . x , self . y )
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 ]
#[ test ]
fn test_vec2 ( ) {
fn test_vec2 ( ) {
macro_rules ! almost_eq {
macro_rules ! almost_eq {