From c12b772af274da9a2c929a352bf5867e995b5be1 Mon Sep 17 00:00:00 2001 From: Adenilson Cavalcanti Date: Mon, 16 Mar 2015 11:33:38 -0700 Subject: [PATCH 1/3] Implementing Display formatter for Rect type. We need this for dumping DisplayList geometric information on servo. --- src/rect.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/rect.rs b/src/rect.rs index 504d8fd..4059436 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -13,7 +13,7 @@ use num::Zero; use point::Point2D; use size::Size2D; use std::cmp::PartialOrd; -use std::fmt; +use std::fmt::{self, Formatter}; use std::num::NumCast; use std::ops::{Add, Sub, Mul, Div}; @@ -29,6 +29,24 @@ impl fmt::Debug for Rect { } } +impl fmt::Display for Rect { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + // Why this won't compile? rustc will say that neither Point2D or + // Size2D has Debug implemented! + //write!(f, "Rect(Size {:?} at Origin {:?})", self.size, self.origin) + let mut output = String::from_str("Display::Origin x = "); + output.push_str(&(self.origin.x.to_string())); + output.push_str("\ty = "); + output.push_str(&(self.origin.y.to_string())); + output.push_str("\tSize width = "); + output.push_str(&(self.size.width.to_string())); + output.push_str("\theight = "); + output.push_str(&(self.size.height.to_string())); + try!(formatter.write_str(output.as_slice())); + Ok(()) + } +} + pub fn Rect(origin: Point2D, size: Size2D) -> Rect { Rect { origin: origin, From 16db24bde0ff80e01dc624f54e382c7e6928b01b Mon Sep 17 00:00:00 2001 From: Adenilson Cavalcanti Date: Mon, 16 Mar 2015 12:13:26 -0700 Subject: [PATCH 2/3] Implementing Display operators in Size and Point. This will allow to have cleaner code in Rect. --- src/point.rs | 9 ++++++++- src/rect.rs | 13 +------------ src/size.rs | 9 ++++++++- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/point.rs b/src/point.rs index 06d2c07..e5c2494 100644 --- a/src/point.rs +++ b/src/point.rs @@ -11,7 +11,7 @@ use length::Length; use size::Size2D; use num::Zero; -use std::fmt; +use std::fmt::{self, Formatter}; use std::num::NumCast; use std::ops::{Add, Neg, Mul, Sub, Div}; @@ -33,6 +33,13 @@ impl fmt::Debug for Point2D { } } +impl fmt::Display for Point2D { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "({},{})", self.x, self.y); + Ok(()) + } +} + pub fn Point2D(x: T, y: T) -> Point2D { Point2D {x: x, y: y} } diff --git a/src/rect.rs b/src/rect.rs index 4059436..b10dbad 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -31,18 +31,7 @@ impl fmt::Debug for Rect { impl fmt::Display for Rect { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - // Why this won't compile? rustc will say that neither Point2D or - // Size2D has Debug implemented! - //write!(f, "Rect(Size {:?} at Origin {:?})", self.size, self.origin) - let mut output = String::from_str("Display::Origin x = "); - output.push_str(&(self.origin.x.to_string())); - output.push_str("\ty = "); - output.push_str(&(self.origin.y.to_string())); - output.push_str("\tSize width = "); - output.push_str(&(self.size.width.to_string())); - output.push_str("\theight = "); - output.push_str(&(self.size.height.to_string())); - try!(formatter.write_str(output.as_slice())); + write!(formatter, "Rect(Size {} at Origin {})", self.size.to_string(), self.origin.to_string()); Ok(()) } } diff --git a/src/size.rs b/src/size.rs index 75740b2..ccd0acb 100644 --- a/src/size.rs +++ b/src/size.rs @@ -10,7 +10,7 @@ use length::Length; use num::Zero; -use std::fmt; +use std::fmt::{self, Formatter}; use std::num::NumCast; use std::ops::{Mul, Div}; @@ -26,6 +26,13 @@ impl fmt::Debug for Size2D { } } +impl fmt::Display for Size2D { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "({},{})", self.width, self.height); + Ok(()) + } +} + pub fn Size2D(width: T, height: T) -> Size2D { Size2D { width: width, From c92f0574de50915b8562d72525af8cde221473ac Mon Sep 17 00:00:00 2001 From: Adenilson Cavalcanti Date: Mon, 16 Mar 2015 14:43:37 -0700 Subject: [PATCH 3/3] Addressing reviewer's comments. --- src/rect.rs | 2 +- src/size.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rect.rs b/src/rect.rs index b10dbad..7e0a8c0 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -31,7 +31,7 @@ impl fmt::Debug for Rect { impl fmt::Display for Rect { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "Rect(Size {} at Origin {})", self.size.to_string(), self.origin.to_string()); + write!(formatter, "Rect({} at {})", self.size.to_string(), self.origin.to_string()); Ok(()) } } diff --git a/src/size.rs b/src/size.rs index ccd0acb..e34a218 100644 --- a/src/size.rs +++ b/src/size.rs @@ -28,7 +28,7 @@ impl fmt::Debug for Size2D { impl fmt::Display for Size2D { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - write!(formatter, "({},{})", self.width, self.height); + write!(formatter, "({}x{})", self.width, self.height); Ok(()) } }