File tree Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Expand file tree Collapse file tree 1 file changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -871,16 +871,33 @@ impl<I: Iterator + ?Sized> Iterator for Box<I> {
871
871
fn nth ( & mut self , n : usize ) -> Option < I :: Item > {
872
872
( * * self ) . nth ( n)
873
873
}
874
+ fn last ( self ) -> Option < I :: Item > {
875
+ BoxIter :: last ( self )
876
+ }
877
+ }
878
+
879
+ trait BoxIter {
880
+ type Item ;
881
+ fn last ( self ) -> Option < Self :: Item > ;
882
+ }
883
+
884
+ impl < I : Iterator + ?Sized > BoxIter for Box < I > {
885
+ type Item = I :: Item ;
874
886
default fn last ( self ) -> Option < I :: Item > {
875
- let mut last = None ;
876
- for x in self { last = Some ( x) ; }
877
- last
887
+ #[ inline]
888
+ fn some < T > ( _: Option < T > , x : T ) -> Option < T > {
889
+ Some ( x)
890
+ }
891
+
892
+ self . fold ( None , some)
878
893
}
879
894
}
880
895
896
+ /// Specialization for sized `I`s that uses `I`s implementation of `last()`
897
+ /// instead of the default.
881
898
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
882
- impl < I : Iterator + Sized > Iterator for Box < I > {
883
- fn last ( self ) -> Option < I :: Item > where I : Sized {
899
+ impl < I : Iterator > BoxIter for Box < I > {
900
+ fn last ( self ) -> Option < I :: Item > {
884
901
( * self ) . last ( )
885
902
}
886
903
}
You can’t perform that action at this time.
0 commit comments