From 1341116d1498c0a12d1c20aff45bbcea632abca5 Mon Sep 17 00:00:00 2001 From: CAD97 Date: Sun, 8 Mar 2020 14:34:47 -0400 Subject: [PATCH] Provide generic ref-removing ops for TextSize --- lib/text-size/src/size.rs | 58 ++++++++++++++++++++----------------- lib/text-size/tests/main.rs | 2 +- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/text-size/src/size.rs b/lib/text-size/src/size.rs index 867a0004ae..454327a015 100644 --- a/lib/text-size/src/size.rs +++ b/lib/text-size/src/size.rs @@ -100,46 +100,52 @@ impl From for usize { } } -macro_rules! arith { - ($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => { +macro_rules! ops { + (impl $Op:ident for TextSize by fn $f:ident = $op:tt) => { impl $Op for TextSize { type Output = TextSize; - fn $op(self, rhs: TextSize) -> TextSize { - TextSize($Op::$op(self.raw, rhs.raw)) + fn $f(self, other: TextSize) -> TextSize { + TextSize(self.raw $op other.raw) } } - impl $Op for &'_ TextSize { + impl $Op<&TextSize> for TextSize { type Output = TextSize; - fn $op(self, rhs: TextSize) -> TextSize { - TextSize($Op::$op(self.raw, rhs.raw)) + fn $f(self, other: &TextSize) -> TextSize { + self $op *other } } - impl $Op<&'_ TextSize> for TextSize { - type Output = TextSize; - fn $op(self, rhs: &TextSize) -> TextSize { - TextSize($Op::$op(self.raw, rhs.raw)) - } - } - impl $Op<&'_ TextSize> for &'_ TextSize { - type Output = TextSize; - fn $op(self, rhs: &TextSize) -> TextSize { - TextSize($Op::$op(self.raw, rhs.raw)) - } - } - - impl $OpAssign for TextSize + impl $Op for &TextSize where - TextSize: $Op, + TextSize: $Op, { - fn $op_assign(&mut self, rhs: A) { - *self = $Op::$op(*self, rhs) + type Output = TextSize; + fn $f(self, other: T) -> TextSize { + *self $op other } } }; } -arith!(Add add, AddAssign add_assign); -arith!(Sub sub, SubAssign sub_assign); +ops!(impl Add for TextSize by fn add = +); +ops!(impl Sub for TextSize by fn sub = -); + +impl AddAssign for TextSize +where + TextSize: Add, +{ + fn add_assign(&mut self, rhs: A) { + *self = *self + rhs + } +} + +impl SubAssign for TextSize +where + TextSize: Sub, +{ + fn sub_assign(&mut self, rhs: S) { + *self = *self - rhs + } +} impl iter::Sum for TextSize where diff --git a/lib/text-size/tests/main.rs b/lib/text-size/tests/main.rs index 9a20cf9819..66b6106671 100644 --- a/lib/text-size/tests/main.rs +++ b/lib/text-size/tests/main.rs @@ -11,7 +11,7 @@ fn range(x: ops::Range) -> TextRange { #[test] fn sum() { let xs: Vec = vec![size(0), size(1), size(2)]; - assert_eq!(xs.iter().copied().sum::(), size(3)); + assert_eq!(xs.iter().sum::(), size(3)); assert_eq!(xs.into_iter().sum::(), size(3)); }