From 6cd86b942012c98c645b975f398bd5253118e3e7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sat, 16 Jan 2021 13:59:40 +0100 Subject: [PATCH] Implement point operators with macro --- src/common.rs | 118 +++++++++++++++------------------------------------------- 1 file changed, 29 insertions(+), 89 deletions(-) diff --git a/src/common.rs b/src/common.rs index f91ec83..c75274c 100644 --- a/src/common.rs +++ b/src/common.rs @@ -21,59 +21,38 @@ impl Point2D { } } -////////// add point to point ////////////////////////////////////////////////// -impl> Add for Point2D { - type Output = Self; - - fn add(self, rhs: Self) -> Self { - Self { - x: self.x + rhs.x, - y: self.y + rhs.y, +macro_rules! point_op { + ($op:tt, $trait:ident($fn:ident), $trait_assign:ident($fn_assign:ident), $rhs:ident = $Rhs:ty => $x:expr, $y:expr) => { + impl> $trait<$Rhs> for Point2D { + type Output = Self; + + fn $fn(self, $rhs: $Rhs) -> Self { + Self { + x: self.x $op $x, + y: self.y $op $y, + } + } } - } -} - -impl + Copy> AddAssign for Point2D { - fn add_assign(&mut self, rhs: Self) { - *self = Self { - x: self.x + rhs.x, - y: self.y + rhs.y, - } - } -} -////////// add tuple to point ////////////////////////////////////////////////// -impl> Add<(T, T)> for Point2D { - type Output = Self; - - fn add(self, rhs: (T, T)) -> Self { - Self { - x: self.x + rhs.0, - y: self.y + rhs.1, + impl + Copy> $trait_assign<$Rhs> for Point2D { + fn $fn_assign(&mut self, $rhs: $Rhs) { + *self = Self { + x: self.x $op $x, + y: self.y $op $y, + } + } } } } -////////// subtract point from point /////////////////////////////////////////// -impl> Sub for Point2D { - type Output = Self; - - fn sub(self, rhs: Self) -> Self { - Self { - x: self.x - rhs.x, - y: self.y - rhs.y, - } - } -} - -impl + Copy> SubAssign for Point2D { - fn sub_assign(&mut self, rhs: Self) { - *self = Self { - x: self.x - rhs.x, - y: self.y - rhs.y, - } - } -} +point_op!(+, Add(add), AddAssign(add_assign), rhs = Point2D => rhs.x, rhs.y); +point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = Point2D => rhs.x, rhs.y); +point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = Point2D => rhs.x, rhs.y); +point_op!(/, Div(div), DivAssign(div_assign), rhs = Point2D => rhs.x, rhs.y); +point_op!(+, Add(add), AddAssign(add_assign), rhs = (T, T) => rhs.0, rhs.1); +point_op!(-, Sub(sub), SubAssign(sub_assign), rhs = (T, T) => rhs.0, rhs.1); +point_op!(*, Mul(mul), MulAssign(mul_assign), rhs = (T, T) => rhs.0, rhs.1); +point_op!(/, Div(div), DivAssign(div_assign), rhs = (T, T) => rhs.0, rhs.1); ////////// multiply point with scalar ////////////////////////////////////////// impl + Copy> Mul for Point2D { @@ -96,27 +75,6 @@ impl + Copy> MulAssign for Point2D { } } -////////// multiply components of two points /////////////////////////////////// -impl> Mul for Point2D { - type Output = Self; - - fn mul(self, rhs: Self) -> Self { - Self { - x: self.x * rhs.x, - y: self.y * rhs.y, - } - } -} - -impl + Copy> MulAssign for Point2D { - fn mul_assign(&mut self, rhs: Self) { - *self = Self { - x: self.x * rhs.x, - y: self.y * rhs.y, - } - } -} - ////////// divide point with scalar //////////////////////////////////////////// impl + Copy> Div for Point2D { type Output = Self; @@ -138,27 +96,6 @@ impl + Copy> DivAssign for Point2D { } } -////////// divide components of two points ///////////////////////////////////// -impl> Div for Point2D { - type Output = Self; - - fn div(self, rhs: Self) -> Self { - Self { - x: self.x / rhs.x, - y: self.y / rhs.y, - } - } -} - -impl + Copy> DivAssign for Point2D { - fn div_assign(&mut self, rhs: Self) { - *self = Self { - x: self.x / rhs.x, - y: self.y / rhs.y, - } - } -} - impl> Neg for Point2D { type Output = Self; @@ -236,6 +173,7 @@ mod tests { assert_eq!(a - point!(2, 2), point!(-1, -2)); a -= point!(2, 2); assert_eq!(a, point!(-1, -2)); + assert_eq!(point!(1, 0) - (2, 3), point!(-1, -3)); } #[test] @@ -247,6 +185,7 @@ mod tests { assert_eq!(a, point!(2, 4)); a *= point!(3, 1); assert_eq!(a, point!(6, 4)); + assert_eq!(point!(1, 0) * (2, 3), point!(2, 0)); } #[test] @@ -258,6 +197,7 @@ mod tests { assert_eq!(a, point!(2, 4)); a /= point!(2, 4); assert_eq!(a, point!(1, 1)); + assert_eq!(point!(6, 3) / (2, 3), point!(3, 1)); } #[test] -- 2.11.0