Skip to content

Commit 20486e9

Browse files
committed
Add more impls
1 parent 6f4c8ed commit 20486e9

File tree

2 files changed

+60
-14
lines changed

2 files changed

+60
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "text_unit"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["Aleksey Kladov <[email protected]>"]
55
description = "Newtypes for text offsets"
66
license = "MIT OR Apache-2.0"

src/lib.rs

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "serde")]
22
extern crate serde;
33

4-
use std::{fmt, ops};
4+
use std::{fmt, ops, iter};
55

66

77
/// An offset into text.
@@ -52,29 +52,63 @@ impl From<u32> for TextUnit {
5252
}
5353
}
5454

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 {
5666
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)
5969
}
6070
}
6171

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)
6576
}
6677
}
6778

68-
impl ops::Sub<TextUnit> for TextUnit {
79+
impl<'a, 'b> ops::$T<&'a TextUnit> for &'b TextUnit {
6980
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)
7283
}
7384
}
7485

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)
78112
}
79113
}
80114

@@ -184,3 +218,15 @@ mod serde_impls {
184218
}
185219
}
186220
}
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

Comments
 (0)