From 552df9c47985f964334bf5a5c96a693814843e8d Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Thu, 2 Oct 2014 10:18:05 -0700 Subject: [PATCH] Add unary negation support for Point2D and Length This will allow doing things like rect.translate(&-point), which make the code much simpler and easier to read. --- src/length.rs | 18 ++++++++++++++++++ src/point.rs | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/src/length.rs b/src/length.rs index 6ce99fa..1185fb0 100644 --- a/src/length.rs +++ b/src/length.rs @@ -64,6 +64,14 @@ impl> Div, Length> Neg> for Length { + #[inline] + fn neg(&self) -> Length { + Length(-self.get()) + } +} + impl Length { /// Cast from one numeric representation to another, preserving the units. pub fn cast(&self) -> Option> { @@ -151,5 +159,15 @@ mod tests { let int_foot: Length = one_foot.cast().unwrap(); assert_eq!(int_foot.get(), 12); + + let negative_one_foot = -one_foot; + assert_eq!(negative_one_foot.get(), -12.0); + + let negative_two_feet = -two_feet; + assert_eq!(negative_two_feet.get(), -24.0); + + let zero_feet: Length = Length(0.0); + let negative_zero_feet = -zero_feet; + assert_eq!(negative_zero_feet.get(), 0.0); } } diff --git a/src/point.rs b/src/point.rs index 8d4213c..67730a6 100644 --- a/src/point.rs +++ b/src/point.rs @@ -58,6 +58,13 @@ impl> Sub, Point2D> for Point2D { } } +impl > Neg> for Point2D { + #[inline] + fn neg(&self) -> Point2D { + Point2D(-self.x, -self.y) + } +} + impl, T1: Clone> Mul> for Point2D { #[inline] fn mul(&self, scale: &Scale) -> Point2D {