@@ -13,7 +13,7 @@ use mem;
13
13
use ops:: { self , Add , Sub } ;
14
14
use usize;
15
15
16
- use super :: { FusedIterator , TrustedLen } ;
16
+ use super :: { FusedIterator , TrustedLen , Sum } ;
17
17
18
18
/// Objects that can be stepped over in both directions.
19
19
///
@@ -177,6 +177,20 @@ step_impl_signed!([i64: u64]);
177
177
step_impl_no_between ! ( u64 i64 ) ;
178
178
step_impl_no_between ! ( u128 i128 ) ;
179
179
180
+ macro_rules! range_inc_iter_impl {
181
+ ( $( $t: ty) * ) => ( $(
182
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
183
+ impl Iterator for ops:: RangeInclusive <$t> {
184
+ #[ inline]
185
+ fn sum<S >( self ) -> S where S : Sum <$t> {
186
+ let a = self . start;
187
+ let b = self . end;
188
+ S :: sum( super :: once( ( a + b) * ( 1 + b - a) / 2 ) )
189
+ }
190
+ }
191
+ ) * )
192
+ }
193
+
180
194
macro_rules! range_exact_iter_impl {
181
195
( $( $t: ty) * ) => ( $(
182
196
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -251,8 +265,18 @@ impl<A: Step> Iterator for ops::Range<A> {
251
265
self . start = self . end . clone ( ) ;
252
266
None
253
267
}
268
+
269
+ #[ inline]
270
+ fn max ( self ) -> Option < A > {
271
+ if self . start != self . end {
272
+ Some ( self . end . sub_one ( ) )
273
+ } else { None }
274
+ }
254
275
}
255
276
277
+ // These macros generate specialisations for `Iterator` methods for efficiency purposes.
278
+ range_inc_iter_impl ! ( usize u8 u16 u32 u64 isize i8 i16 i32 i64 ) ;
279
+
256
280
// These macros generate `ExactSizeIterator` impls for various range types.
257
281
// Range<{u,i}64> and RangeInclusive<{u,i}{32,64,size}> are excluded
258
282
// because they cannot guarantee having a length <= usize::MAX, which is
0 commit comments