Skip to content

Commit

Permalink
Provide the same set of impls as stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Mar 10, 2020
1 parent ea66e56 commit 8371915
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<A> AddAssign<A> for TextSize
where
TextSize: Add<A, Output = TextSize>,
{
fn add_assign(&mut self, rhs: A) {
*self = *self + rhs
}
}
macro_rules! arith {
($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => {
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 $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<A> $OpAssign<A> for TextSize
where
TextSize: $Op<A, Output = TextSize>,
{
fn $op_assign(&mut self, rhs: A) {
*self = $Op::$op(*self, rhs)
}
}
};
}

impl<S> SubAssign<S> for TextSize
where
TextSize: Sub<S, Output = TextSize>,
{
fn sub_assign(&mut self, rhs: S) {
*self = *self - rhs
}
}
arith!(Add add, AddAssign add_assign);
arith!(Sub sub, SubAssign sub_assign);

impl<A> iter::Sum<A> for TextSize
where
Expand Down

0 comments on commit 8371915

Please sign in to comment.