File tree 3 files changed +18
-25
lines changed
3 files changed +18
-25
lines changed Original file line number Diff line number Diff line change @@ -203,9 +203,7 @@ pub trait Iterator {
203
203
#[ inline]
204
204
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
205
205
fn last ( self ) -> Option < Self :: Item > where Self : Sized {
206
- let mut last = None ;
207
- for x in self { last = Some ( x) ; }
208
- last
206
+ SpecLast :: last ( self )
209
207
}
210
208
211
209
/// Returns the `n`th element of the iterator.
@@ -2239,3 +2237,20 @@ impl<'a, I: Iterator + ?Sized> Iterator for &'a mut I {
2239
2237
( * * self ) . nth ( n)
2240
2238
}
2241
2239
}
2240
+
2241
+ /// Allows specialization for `Iterator::last`.
2242
+ trait SpecLast : Iterator + Sized {
2243
+ fn last ( self ) -> Option < Self :: Item > ;
2244
+ }
2245
+ impl < T : Iterator > SpecLast for T {
2246
+ default fn last ( self ) -> Option < Self :: Item > {
2247
+ let mut last = None ;
2248
+ for x in self { last = Some ( x) ; }
2249
+ last
2250
+ }
2251
+ }
2252
+ impl < T : DoubleEndedIterator > SpecLast for T {
2253
+ fn last ( mut self ) -> Option < Self :: Item > {
2254
+ self . next_back ( )
2255
+ }
2256
+ }
Original file line number Diff line number Diff line change @@ -1159,11 +1159,6 @@ macro_rules! iterator {
1159
1159
self . iter_nth( n)
1160
1160
}
1161
1161
1162
- #[ inline]
1163
- fn last( mut self ) -> Option <$elem> {
1164
- self . next_back( )
1165
- }
1166
-
1167
1162
fn all<F >( & mut self , mut predicate: F ) -> bool
1168
1163
where F : FnMut ( Self :: Item ) -> bool ,
1169
1164
{
Original file line number Diff line number Diff line change @@ -542,12 +542,6 @@ impl<'a> Iterator for Chars<'a> {
542
542
// `isize::MAX` (that's well below `usize::MAX`).
543
543
( ( len + 3 ) / 4 , Some ( len) )
544
544
}
545
-
546
- #[ inline]
547
- fn last ( mut self ) -> Option < char > {
548
- // No need to go through the entire string.
549
- self . next_back ( )
550
- }
551
545
}
552
546
553
547
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -634,12 +628,6 @@ impl<'a> Iterator for CharIndices<'a> {
634
628
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
635
629
self . iter . size_hint ( )
636
630
}
637
-
638
- #[ inline]
639
- fn last ( mut self ) -> Option < ( usize , char ) > {
640
- // No need to go through the entire string.
641
- self . next_back ( )
642
- }
643
631
}
644
632
645
633
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -701,11 +689,6 @@ impl<'a> Iterator for Bytes<'a> {
701
689
self . 0 . count ( )
702
690
}
703
691
704
- #[ inline]
705
- fn last ( self ) -> Option < Self :: Item > {
706
- self . 0 . last ( )
707
- }
708
-
709
692
#[ inline]
710
693
fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
711
694
self . 0 . nth ( n)
You can’t perform that action at this time.
0 commit comments