Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Display impl for Vec2, Pos2, and Rect #4428

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions crates/emath/src/pos2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};

use crate::*;
Expand Down Expand Up @@ -316,8 +317,19 @@ impl Div<f32> 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(())
}
}
16 changes: 14 additions & 2 deletions crates/emath/src/rect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::f32::INFINITY;
use std::fmt;

use crate::*;

Expand Down Expand Up @@ -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]
Expand Down
16 changes: 14 additions & 2 deletions crates/emath/src/vec2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use crate::Vec2b;
Expand Down Expand Up @@ -464,12 +465,23 @@ impl Div<f32> 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 {
Expand Down
Loading