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 @@ -883,16 +883,33 @@ impl<I: Iterator + ?Sized> Iterator for Box<I> {
883
883
fn nth ( & mut self , n : usize ) -> Option < I :: Item > {
884
884
( * * self ) . nth ( n)
885
885
}
886
+ fn last ( self ) -> Option < I :: Item > {
887
+ BoxIter :: last ( self )
888
+ }
889
+ }
890
+
891
+ trait BoxIter {
892
+ type Item ;
893
+ fn last ( self ) -> Option < Self :: Item > ;
894
+ }
895
+
896
+ impl < I : Iterator + ?Sized > BoxIter for Box < I > {
897
+ type Item = I :: Item ;
886
898
default fn last ( self ) -> Option < I :: Item > {
887
- let mut last = None ;
888
- for x in self { last = Some ( x) ; }
889
- last
899
+ #[ inline]
900
+ fn some < T > ( _: Option < T > , x : T ) -> Option < T > {
901
+ Some ( x)
902
+ }
903
+
904
+ self . fold ( None , some)
890
905
}
891
906
}
892
907
908
+ /// Specialization for sized `I`s that uses `I`s implementation of `last()`
909
+ /// instead of the default.
893
910
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
894
- impl < I : Iterator + Sized > Iterator for Box < I > {
895
- fn last ( self ) -> Option < I :: Item > where I : Sized {
911
+ impl < I : Iterator > BoxIter for Box < I > {
912
+ fn last ( self ) -> Option < I :: Item > {
896
913
( * self ) . last ( )
897
914
}
898
915
}
You can’t perform that action at this time.
0 commit comments