File tree 2 files changed +28
-9
lines changed 2 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -249,8 +249,32 @@ impl<A: Step> Iterator for ops::Range<A> where
249
249
} )
250
250
}
251
251
252
- // TODO: last specialisation if `A: Sub` and/or Range<A> is a DoubleEndedIterator.
253
- // so self.end.sub_one() or self.next_back()
252
+ #[ inline]
253
+ fn last ( self ) -> Option < Self :: Item > {
254
+ <Self as SpecLast >:: last ( self )
255
+ }
256
+ }
257
+
258
+ trait SpecLast {
259
+ type Item ;
260
+ fn last ( self ) -> Option < Self :: Item > ;
261
+ }
262
+
263
+ impl < It > SpecLast for It
264
+ where It : Iterator {
265
+ type Item = <Self as Iterator >:: Item ;
266
+ #[ inline]
267
+ default fn last ( self ) -> Option < Self :: Item > {
268
+ <Self as Iterator >:: last ( self )
269
+ }
270
+ }
271
+
272
+ impl < It > SpecLast for It
273
+ where It : DoubleEndedIterator {
274
+ #[ inline]
275
+ fn last ( mut self ) -> Option < Self :: Item > {
276
+ <Self as DoubleEndedIterator >:: next_back ( & mut self )
277
+ }
254
278
}
255
279
256
280
// These macros generate `ExactSizeIterator` impls for various range types.
@@ -319,7 +343,7 @@ impl<A: Step> Iterator for ops::RangeFrom<A> where
319
343
320
344
#[ cold]
321
345
fn last ( self ) -> Option < Self :: Item > {
322
- panic ! ( "Iterator::last on a `x..` cannot work " )
346
+ panic ! ( "Iterator::last on a `x..` will overflow " )
323
347
}
324
348
}
325
349
Original file line number Diff line number Diff line change @@ -1157,12 +1157,6 @@ fn test_usize_range_no_last() {
1157
1157
assert_eq ! ( ( 0 ..) . last( ) , None ) ;
1158
1158
}
1159
1159
1160
- #[ test]
1161
- #[ should_panic]
1162
- fn test_usize_range_no_last ( ) {
1163
- assert_eq ! ( ( 0 ..) . last( ) , None ) ;
1164
- }
1165
-
1166
1160
#[ test]
1167
1161
#[ should_panic]
1168
1162
fn test_isize_range_count_oflow ( ) {
@@ -1172,6 +1166,7 @@ fn test_isize_range_count_oflow() {
1172
1166
#[ test]
1173
1167
fn test_range_last ( ) {
1174
1168
assert_eq ! ( ( 0 ..10 ) . last( ) , Some ( 9 ) ) ;
1169
+ assert_eq ! ( ( 0u32 ..!0 ) . last( ) , Some ( !0 - 1 ) ) ;
1175
1170
assert_eq ! ( RangeInclusive { start: 0 , end: 10 } . last( ) , Some ( 10 ) ) ;
1176
1171
}
1177
1172
You can’t perform that action at this time.
0 commit comments