@@ -57,7 +57,7 @@ impl From<u32> for TextUnit {
57
57
}
58
58
}
59
59
60
- macro_rules! ops_impls {
60
+ macro_rules! unit_ops_impls {
61
61
( $T: ident, $f: ident, $op: tt, $AT: ident, $af: ident) => {
62
62
63
63
impl ops:: $T<TextUnit > for TextUnit {
@@ -108,8 +108,73 @@ impl<'a> ops::$AT<&'a TextUnit> for TextUnit {
108
108
} ;
109
109
}
110
110
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) ;
113
178
114
179
impl < ' a > iter:: Sum < & ' a TextUnit > for TextUnit {
115
180
fn sum < I : Iterator < Item =& ' a TextUnit > > ( iter : I ) -> TextUnit {
@@ -246,4 +311,18 @@ mod tests {
246
311
assert_eq ! ( xs. iter( ) . sum:: <TextUnit >( ) , 3 . into( ) ) ;
247
312
assert_eq ! ( xs. into_iter( ) . sum:: <TextUnit >( ) , 3 . into( ) ) ;
248
313
}
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
+ }
249
328
}
0 commit comments