Skip to content

Commit 8371915

Browse files
committed
Provide the same set of impls as stdlib
1 parent ea66e56 commit 8371915

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

src/size.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -142,38 +142,46 @@ conversions! {
142142
varries [usize]
143143
}
144144

145-
// NB: We do not provide the transparent-ref impls like the stdlib does.
146-
impl Add for TextSize {
147-
type Output = TextSize;
148-
fn add(self, rhs: TextSize) -> TextSize {
149-
TextSize(self.raw + rhs.raw)
150-
}
151-
}
152-
153-
impl<A> AddAssign<A> for TextSize
154-
where
155-
TextSize: Add<A, Output = TextSize>,
156-
{
157-
fn add_assign(&mut self, rhs: A) {
158-
*self = *self + rhs
159-
}
160-
}
145+
macro_rules! arith {
146+
($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => {
147+
impl $Op<TextSize> for TextSize {
148+
type Output = TextSize;
149+
fn $op(self, rhs: TextSize) -> TextSize {
150+
TextSize($Op::$op(self.raw, rhs.raw))
151+
}
152+
}
153+
impl $Op<TextSize> for &'_ TextSize {
154+
type Output = TextSize;
155+
fn $op(self, rhs: TextSize) -> TextSize {
156+
TextSize($Op::$op(self.raw, rhs.raw))
157+
}
158+
}
159+
impl $Op<&'_ TextSize> for TextSize {
160+
type Output = TextSize;
161+
fn $op(self, rhs: &TextSize) -> TextSize {
162+
TextSize($Op::$op(self.raw, rhs.raw))
163+
}
164+
}
165+
impl $Op<&'_ TextSize> for &'_ TextSize {
166+
type Output = TextSize;
167+
fn $op(self, rhs: &TextSize) -> TextSize {
168+
TextSize($Op::$op(self.raw, rhs.raw))
169+
}
170+
}
161171

162-
impl Sub for TextSize {
163-
type Output = TextSize;
164-
fn sub(self, rhs: TextSize) -> TextSize {
165-
TextSize(self.raw - rhs.raw)
166-
}
172+
impl<A> $OpAssign<A> for TextSize
173+
where
174+
TextSize: $Op<A, Output = TextSize>,
175+
{
176+
fn $op_assign(&mut self, rhs: A) {
177+
*self = $Op::$op(*self, rhs)
178+
}
179+
}
180+
};
167181
}
168182

169-
impl<S> SubAssign<S> for TextSize
170-
where
171-
TextSize: Sub<S, Output = TextSize>,
172-
{
173-
fn sub_assign(&mut self, rhs: S) {
174-
*self = *self - rhs
175-
}
176-
}
183+
arith!(Add add, AddAssign add_assign);
184+
arith!(Sub sub, SubAssign sub_assign);
177185

178186
impl<A> iter::Sum<A> for TextSize
179187
where

0 commit comments

Comments
 (0)