@@ -1045,6 +1045,11 @@ impl<A, B> Iterator for Zip<A, B> where A: Iterator, B: Iterator
1045
1045
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1046
1046
ZipImpl :: size_hint ( self )
1047
1047
}
1048
+
1049
+ #[ inline]
1050
+ fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
1051
+ ZipImpl :: nth ( self , n)
1052
+ }
1048
1053
}
1049
1054
1050
1055
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -1065,6 +1070,14 @@ trait ZipImpl<A, B> {
1065
1070
fn new ( a : A , b : B ) -> Self ;
1066
1071
fn next ( & mut self ) -> Option < Self :: Item > ;
1067
1072
fn size_hint ( & self ) -> ( usize , Option < usize > ) ;
1073
+ fn nth ( & mut self , n : usize ) -> Option < Self :: Item > ;
1074
+ fn super_nth ( & mut self , mut n : usize ) -> Option < Self :: Item > {
1075
+ while let Some ( x) = self . next ( ) {
1076
+ if n == 0 { return Some ( x) }
1077
+ n -= 1 ;
1078
+ }
1079
+ None
1080
+ }
1068
1081
fn next_back ( & mut self ) -> Option < Self :: Item >
1069
1082
where A : DoubleEndedIterator + ExactSizeIterator ,
1070
1083
B : DoubleEndedIterator + ExactSizeIterator ;
@@ -1094,6 +1107,11 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
1094
1107
} )
1095
1108
}
1096
1109
1110
+ #[ inline]
1111
+ default fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
1112
+ self . super_nth ( n)
1113
+ }
1114
+
1097
1115
#[ inline]
1098
1116
default fn next_back ( & mut self ) -> Option < ( A :: Item , B :: Item ) >
1099
1117
where A : DoubleEndedIterator + ExactSizeIterator ,
@@ -1174,6 +1192,24 @@ impl<A, B> ZipImpl<A, B> for Zip<A, B>
1174
1192
( len, Some ( len) )
1175
1193
}
1176
1194
1195
+ #[ inline]
1196
+ fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
1197
+ let delta = cmp:: min ( n, self . len - self . index ) ;
1198
+ let end = self . index + delta;
1199
+ while self . index < end {
1200
+ let i = self . index ;
1201
+ self . index += 1 ;
1202
+ if A :: may_have_side_effect ( ) {
1203
+ unsafe { self . a . get_unchecked ( i) ; }
1204
+ }
1205
+ if B :: may_have_side_effect ( ) {
1206
+ unsafe { self . b . get_unchecked ( i) ; }
1207
+ }
1208
+ }
1209
+
1210
+ self . super_nth ( n - delta)
1211
+ }
1212
+
1177
1213
#[ inline]
1178
1214
fn next_back ( & mut self ) -> Option < ( A :: Item , B :: Item ) >
1179
1215
where A : DoubleEndedIterator + ExactSizeIterator ,
0 commit comments