Skip to content

Commit 3d5a1ba

Browse files
bors[bot]CAD97
andauthored
Merge #8
8: Provide ref-transparent ops for TextSize r=matklad a=CAD97 Counter-proposal to #7. Basically the same set of impls, but set up slightly differently. Co-authored-by: CAD97 <[email protected]>
2 parents fc9b258 + ed056c4 commit 3d5a1ba

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/size.rs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,46 +100,52 @@ impl From<TextSize> for usize {
100100
}
101101
}
102102

103-
macro_rules! arith {
104-
($Op:ident $op:ident, $OpAssign:ident $op_assign:ident) => {
103+
macro_rules! ops {
104+
(impl $Op:ident for TextSize by fn $f:ident = $op:tt) => {
105105
impl $Op<TextSize> for TextSize {
106106
type Output = TextSize;
107-
fn $op(self, rhs: TextSize) -> TextSize {
108-
TextSize($Op::$op(self.raw, rhs.raw))
107+
fn $f(self, other: TextSize) -> TextSize {
108+
TextSize(self.raw $op other.raw)
109109
}
110110
}
111-
impl $Op<TextSize> for &'_ TextSize {
111+
impl $Op<&TextSize> for TextSize {
112112
type Output = TextSize;
113-
fn $op(self, rhs: TextSize) -> TextSize {
114-
TextSize($Op::$op(self.raw, rhs.raw))
113+
fn $f(self, other: &TextSize) -> TextSize {
114+
self $op *other
115115
}
116116
}
117-
impl $Op<&'_ TextSize> for TextSize {
118-
type Output = TextSize;
119-
fn $op(self, rhs: &TextSize) -> TextSize {
120-
TextSize($Op::$op(self.raw, rhs.raw))
121-
}
122-
}
123-
impl $Op<&'_ TextSize> for &'_ TextSize {
124-
type Output = TextSize;
125-
fn $op(self, rhs: &TextSize) -> TextSize {
126-
TextSize($Op::$op(self.raw, rhs.raw))
127-
}
128-
}
129-
130-
impl<A> $OpAssign<A> for TextSize
117+
impl<T> $Op<T> for &TextSize
131118
where
132-
TextSize: $Op<A, Output = TextSize>,
119+
TextSize: $Op<T, Output=TextSize>,
133120
{
134-
fn $op_assign(&mut self, rhs: A) {
135-
*self = $Op::$op(*self, rhs)
121+
type Output = TextSize;
122+
fn $f(self, other: T) -> TextSize {
123+
*self $op other
136124
}
137125
}
138126
};
139127
}
140128

141-
arith!(Add add, AddAssign add_assign);
142-
arith!(Sub sub, SubAssign sub_assign);
129+
ops!(impl Add for TextSize by fn add = +);
130+
ops!(impl Sub for TextSize by fn sub = -);
131+
132+
impl<A> AddAssign<A> for TextSize
133+
where
134+
TextSize: Add<A, Output = TextSize>,
135+
{
136+
fn add_assign(&mut self, rhs: A) {
137+
*self = *self + rhs
138+
}
139+
}
140+
141+
impl<S> SubAssign<S> for TextSize
142+
where
143+
TextSize: Sub<S, Output = TextSize>,
144+
{
145+
fn sub_assign(&mut self, rhs: S) {
146+
*self = *self - rhs
147+
}
148+
}
143149

144150
impl<A> iter::Sum<A> for TextSize
145151
where

tests/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn range(x: ops::Range<u32>) -> TextRange {
1111
#[test]
1212
fn sum() {
1313
let xs: Vec<TextSize> = vec![size(0), size(1), size(2)];
14-
assert_eq!(xs.iter().copied().sum::<TextSize>(), size(3));
14+
assert_eq!(xs.iter().sum::<TextSize>(), size(3));
1515
assert_eq!(xs.into_iter().sum::<TextSize>(), size(3));
1616
}
1717

0 commit comments

Comments
 (0)