|
1 | 1 | #[cfg(feature = "serde")]
|
2 | 2 | extern crate serde;
|
3 | 3 |
|
4 |
| -use std::{fmt, ops}; |
| 4 | +use std::{fmt, ops, iter}; |
5 | 5 |
|
6 | 6 |
|
7 | 7 | /// An offset into text.
|
@@ -52,29 +52,63 @@ impl From<u32> for TextUnit {
|
52 | 52 | }
|
53 | 53 | }
|
54 | 54 |
|
55 |
| -impl ops::Add<TextUnit> for TextUnit { |
| 55 | +macro_rules! ops_impls { |
| 56 | + ($T:ident, $f:ident, $op:tt, $AT:ident, $af:ident) => { |
| 57 | + |
| 58 | +impl ops::$T<TextUnit> for TextUnit { |
| 59 | + type Output = TextUnit; |
| 60 | + fn $f(self, rhs: TextUnit) -> TextUnit { |
| 61 | + TextUnit(self.0 $op rhs.0) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<'a> ops::$T<&'a TextUnit> for TextUnit { |
56 | 66 | type Output = TextUnit;
|
57 |
| - fn add(self, rhs: TextUnit) -> TextUnit { |
58 |
| - TextUnit(self.0 + rhs.0) |
| 67 | + fn $f(self, rhs: &'a TextUnit) -> TextUnit { |
| 68 | + ops::$T::$f(self, *rhs) |
59 | 69 | }
|
60 | 70 | }
|
61 | 71 |
|
62 |
| -impl ops::AddAssign<TextUnit> for TextUnit { |
63 |
| - fn add_assign(&mut self, rhs: TextUnit) { |
64 |
| - self.0 += rhs.0 |
| 72 | +impl<'a> ops::$T<TextUnit> for &'a TextUnit { |
| 73 | + type Output = TextUnit; |
| 74 | + fn $f(self, rhs: TextUnit) -> TextUnit { |
| 75 | + ops::$T::$f(*self, rhs) |
65 | 76 | }
|
66 | 77 | }
|
67 | 78 |
|
68 |
| -impl ops::Sub<TextUnit> for TextUnit { |
| 79 | +impl<'a, 'b> ops::$T<&'a TextUnit> for &'b TextUnit { |
69 | 80 | type Output = TextUnit;
|
70 |
| - fn sub(self, rhs: TextUnit) -> TextUnit { |
71 |
| - TextUnit(self.0 - rhs.0) |
| 81 | + fn $f(self, rhs: &'a TextUnit) -> TextUnit { |
| 82 | + ops::$T::$f(*self, *rhs) |
72 | 83 | }
|
73 | 84 | }
|
74 | 85 |
|
75 |
| -impl ops::SubAssign<TextUnit> for TextUnit { |
76 |
| - fn sub_assign(&mut self, rhs: TextUnit) { |
77 |
| - self.0 -= rhs.0 |
| 86 | +impl ops::$AT<TextUnit> for TextUnit { |
| 87 | + fn $af(&mut self, rhs: TextUnit) { |
| 88 | + self.0 = self.0 $op rhs.0 |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<'a> ops::$AT<&'a TextUnit> for TextUnit { |
| 93 | + fn $af(&mut self, rhs: &'a TextUnit) { |
| 94 | + ops::$AT::$af(self, *rhs) |
| 95 | + } |
| 96 | +} |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +ops_impls!(Add, add, +, AddAssign, add_assign); |
| 101 | +ops_impls!(Sub, sub, -, SubAssign, sub_assign); |
| 102 | + |
| 103 | +impl<'a> iter::Sum<&'a TextUnit> for TextUnit { |
| 104 | + fn sum<I: Iterator<Item=&'a TextUnit>>(iter: I) -> TextUnit { |
| 105 | + iter.fold(TextUnit::from(0), ops::Add::add) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl iter::Sum<TextUnit> for TextUnit { |
| 110 | + fn sum<I: Iterator<Item=TextUnit>>(iter: I) -> TextUnit { |
| 111 | + iter.fold(TextUnit::from(0), ops::Add::add) |
78 | 112 | }
|
79 | 113 | }
|
80 | 114 |
|
@@ -184,3 +218,15 @@ mod serde_impls {
|
184 | 218 | }
|
185 | 219 | }
|
186 | 220 | }
|
| 221 | + |
| 222 | +#[cfg(test)] |
| 223 | +mod tests { |
| 224 | + use super::*; |
| 225 | + |
| 226 | + #[test] |
| 227 | + fn test_sum() { |
| 228 | + let xs: Vec<TextUnit> = vec![0.into(), 1.into(), 2.into()]; |
| 229 | + assert_eq!(xs.iter().sum::<TextUnit>(), 3.into()); |
| 230 | + assert_eq!(xs.into_iter().sum::<TextUnit>(), 3.into()); |
| 231 | + } |
| 232 | +} |
0 commit comments