Skip to content

Commit 3b379f8

Browse files
committed
ranges arithmetics
1 parent b26dc78 commit 3b379f8

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
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.2"
3+
version = "0.1.3"
44
authors = ["Aleksey Kladov <[email protected]>"]
55
description = "Newtypes for text offsets"
66
license = "MIT OR Apache-2.0"

src/lib.rs

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl From<u32> for TextUnit {
5757
}
5858
}
5959

60-
macro_rules! ops_impls {
60+
macro_rules! unit_ops_impls {
6161
($T:ident, $f:ident, $op:tt, $AT:ident, $af:ident) => {
6262

6363
impl ops::$T<TextUnit> for TextUnit {
@@ -108,8 +108,73 @@ impl<'a> ops::$AT<&'a TextUnit> for TextUnit {
108108
};
109109
}
110110

111-
ops_impls!(Add, add, +, AddAssign, add_assign);
112-
ops_impls!(Sub, sub, -, SubAssign, sub_assign);
111+
macro_rules! range_ops_impls {
112+
($T:ident, $f:ident, $op:tt, $AT:ident, $af:ident) => {
113+
114+
impl ops::$T<TextUnit> for TextRange {
115+
type Output = TextRange;
116+
#[inline(always)]
117+
fn $f(self, rhs: TextUnit) -> TextRange {
118+
TextRange::from_to(
119+
self.start() $op rhs,
120+
self.end() $op rhs,
121+
)
122+
}
123+
}
124+
125+
impl<'a> ops::$T<&'a TextUnit> for TextRange {
126+
type Output = TextRange;
127+
#[inline(always)]
128+
fn $f(self, rhs: &'a TextUnit) -> TextRange {
129+
TextRange::from_to(
130+
self.start() $op rhs,
131+
self.end() $op rhs,
132+
)
133+
}
134+
}
135+
136+
impl<'a> ops::$T<TextUnit> for &'a TextRange {
137+
type Output = TextRange;
138+
#[inline(always)]
139+
fn $f(self, rhs: TextUnit) -> TextRange {
140+
TextRange::from_to(
141+
self.start() $op rhs,
142+
self.end() $op rhs,
143+
)
144+
}
145+
}
146+
147+
impl<'a, 'b> ops::$T<&'a TextUnit> for &'b TextRange {
148+
type Output = TextRange;
149+
#[inline(always)]
150+
fn $f(self, rhs: &'a TextUnit) -> TextRange {
151+
TextRange::from_to(
152+
self.start() $op rhs,
153+
self.end() $op rhs,
154+
)
155+
}
156+
}
157+
158+
impl ops::$AT<TextUnit> for TextRange {
159+
#[inline(always)]
160+
fn $af(&mut self, rhs: TextUnit) {
161+
*self = *self $op rhs
162+
}
163+
}
164+
165+
impl<'a> ops::$AT<&'a TextUnit> for TextRange {
166+
#[inline(always)]
167+
fn $af(&mut self, rhs: &'a TextUnit) {
168+
*self = *self $op rhs
169+
}
170+
}
171+
};
172+
}
173+
174+
unit_ops_impls!(Add, add, +, AddAssign, add_assign);
175+
unit_ops_impls!(Sub, sub, -, SubAssign, sub_assign);
176+
range_ops_impls!(Add, add, +, AddAssign, add_assign);
177+
range_ops_impls!(Sub, sub, -, SubAssign, sub_assign);
113178

114179
impl<'a> iter::Sum<&'a TextUnit> for TextUnit {
115180
fn sum<I: Iterator<Item=&'a TextUnit>>(iter: I) -> TextUnit {
@@ -246,4 +311,18 @@ mod tests {
246311
assert_eq!(xs.iter().sum::<TextUnit>(), 3.into());
247312
assert_eq!(xs.into_iter().sum::<TextUnit>(), 3.into());
248313
}
314+
315+
#[test]
316+
fn test_ops() {
317+
let r = TextRange::from_to(10.into(), 20.into());
318+
let u: TextUnit = 5.into();
319+
assert_eq!(
320+
r + u,
321+
TextRange::from_to(15.into(), 25.into()),
322+
);
323+
assert_eq!(
324+
r - u,
325+
TextRange::from_to(5.into(), 15.into()),
326+
);
327+
}
249328
}

0 commit comments

Comments
 (0)