diff --git a/src/size.rs b/src/size.rs index 43bf19d..a51a279 100644 --- a/src/size.rs +++ b/src/size.rs @@ -142,38 +142,46 @@ conversions! { varries [usize] } -// NB: We do not provide the transparent-ref impls like the stdlib does. -impl Add for TextSize { - type Output = TextSize; - fn add(self, rhs: TextSize) -> TextSize { - TextSize(self.raw + rhs.raw) - } -} - -impl AddAssign for TextSize -where - TextSize: Add, -{ - fn add_assign(&mut self, rhs: A) { - *self = *self + rhs - } -} +macro_rules! arith { + ($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => { + impl $Op for TextSize { + type Output = TextSize; + fn $op(self, rhs: TextSize) -> TextSize { + TextSize($Op::$op(self.raw, rhs.raw)) + } + } + impl $Op 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 $Op<&'_ TextSize> for &'_ TextSize { + type Output = TextSize; + fn $op(self, rhs: &TextSize) -> TextSize { + TextSize($Op::$op(self.raw, rhs.raw)) + } + } -impl Sub for TextSize { - type Output = TextSize; - fn sub(self, rhs: TextSize) -> TextSize { - TextSize(self.raw - rhs.raw) - } + impl $OpAssign for TextSize + where + TextSize: $Op, + { + fn $op_assign(&mut self, rhs: A) { + *self = $Op::$op(*self, rhs) + } + } + }; } -impl SubAssign for TextSize -where - TextSize: Sub, -{ - fn sub_assign(&mut self, rhs: S) { - *self = *self - rhs - } -} +arith!(Add add, AddAssign add_assign); +arith!(Sub sub, SubAssign sub_assign); impl iter::Sum for TextSize where