2 use crate::fmt::{self, Debug};
5 DoubleEndedIterator, ExactSizeIterator, FusedIterator, InPlaceIterable, Iterator, SourceIter,
9 /// An iterator that iterates two other iterators simultaneously.
11 /// This `struct` is created by [`Iterator::zip`]. See its documentation
14 #[must_use = "iterators are lazy and do nothing unless consumed"]
15 #[stable(feature = "rust1", since = "1.0.0")]
16 pub struct Zip<A, B> {
19 // index and len are only used by the specialized version of zip
23 impl<A: Iterator, B: Iterator> Zip<A, B> {
24 pub(in super::super) fn new(a: A, b: B) -> Zip<A, B> {
27 fn super_nth(&mut self, mut n: usize) -> Option<(A::Item, B::Item)> {
28 while let Some(x) = Iterator::next(self) {
38 #[stable(feature = "rust1", since = "1.0.0")]
39 impl<A, B> Iterator for Zip<A, B>
44 type Item = (A::Item, B::Item);
47 fn next(&mut self) -> Option<Self::Item> {
52 fn size_hint(&self) -> (usize, Option<usize>) {
53 ZipImpl::size_hint(self)
57 fn nth(&mut self, n: usize) -> Option<Self::Item> {
62 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
64 Self: TrustedRandomAccess,
66 // SAFETY: `ZipImpl::__iterator_get_unchecked` has same safety
67 // requirements as `Iterator::__iterator_get_unchecked`.
68 unsafe { ZipImpl::get_unchecked(self, idx) }
72 #[stable(feature = "rust1", since = "1.0.0")]
73 impl<A, B> DoubleEndedIterator for Zip<A, B>
75 A: DoubleEndedIterator + ExactSizeIterator,
76 B: DoubleEndedIterator + ExactSizeIterator,
79 fn next_back(&mut self) -> Option<(A::Item, B::Item)> {
80 ZipImpl::next_back(self)
84 // Zip specialization trait
88 fn new(a: A, b: B) -> Self;
89 fn next(&mut self) -> Option<Self::Item>;
90 fn size_hint(&self) -> (usize, Option<usize>);
91 fn nth(&mut self, n: usize) -> Option<Self::Item>;
92 fn next_back(&mut self) -> Option<Self::Item>
94 A: DoubleEndedIterator + ExactSizeIterator,
95 B: DoubleEndedIterator + ExactSizeIterator;
96 // This has the same safety requirements as `Iterator::__iterator_get_unchecked`
97 unsafe fn get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item
99 Self: Iterator + TrustedRandomAccess;
104 impl<A, B> ZipImpl<A, B> for Zip<A, B>
109 type Item = (A::Item, B::Item);
110 default fn new(a: A, b: B) -> Self {
120 default fn next(&mut self) -> Option<(A::Item, B::Item)> {
121 let x = self.a.next()?;
122 let y = self.b.next()?;
127 default fn nth(&mut self, n: usize) -> Option<Self::Item> {
132 default fn next_back(&mut self) -> Option<(A::Item, B::Item)>
134 A: DoubleEndedIterator + ExactSizeIterator,
135 B: DoubleEndedIterator + ExactSizeIterator,
137 let a_sz = self.a.len();
138 let b_sz = self.b.len();
140 // Adjust a, b to equal length
142 for _ in 0..a_sz - b_sz {
146 for _ in 0..b_sz - a_sz {
151 match (self.a.next_back(), self.b.next_back()) {
152 (Some(x), Some(y)) => Some((x, y)),
153 (None, None) => None,
159 default fn size_hint(&self) -> (usize, Option<usize>) {
160 let (a_lower, a_upper) = self.a.size_hint();
161 let (b_lower, b_upper) = self.b.size_hint();
163 let lower = cmp::min(a_lower, b_lower);
165 let upper = match (a_upper, b_upper) {
166 (Some(x), Some(y)) => Some(cmp::min(x, y)),
167 (Some(x), None) => Some(x),
168 (None, Some(y)) => Some(y),
169 (None, None) => None,
175 default unsafe fn get_unchecked(&mut self, _idx: usize) -> <Self as Iterator>::Item
177 Self: TrustedRandomAccess,
179 unreachable!("Always specialized");
184 impl<A, B> ZipImpl<A, B> for Zip<A, B>
186 A: TrustedRandomAccess + Iterator,
187 B: TrustedRandomAccess + Iterator,
189 fn new(a: A, b: B) -> Self {
190 let len = cmp::min(a.size(), b.size());
191 Zip { a, b, index: 0, len }
195 fn next(&mut self) -> Option<(A::Item, B::Item)> {
196 if self.index < self.len {
199 // SAFETY: `i` is smaller than `self.len`, thus smaller than `self.a.len()` and `self.b.len()`
201 Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
203 } else if A::may_have_side_effect() && self.index < self.a.size() {
204 // match the base implementation's potential side effects
205 // SAFETY: we just checked that `self.index` < `self.a.len()`
207 self.a.__iterator_get_unchecked(self.index);
217 fn size_hint(&self) -> (usize, Option<usize>) {
218 let len = self.len - self.index;
223 fn nth(&mut self, n: usize) -> Option<Self::Item> {
224 let delta = cmp::min(n, self.len - self.index);
225 let end = self.index + delta;
226 while self.index < end {
229 if A::may_have_side_effect() {
230 // SAFETY: the usage of `cmp::min` to calculate `delta`
231 // ensures that `end` is smaller than or equal to `self.len`,
232 // so `i` is also smaller than `self.len`.
234 self.a.__iterator_get_unchecked(i);
237 if B::may_have_side_effect() {
238 // SAFETY: same as above.
240 self.b.__iterator_get_unchecked(i);
245 self.super_nth(n - delta)
249 fn next_back(&mut self) -> Option<(A::Item, B::Item)>
251 A: DoubleEndedIterator + ExactSizeIterator,
252 B: DoubleEndedIterator + ExactSizeIterator,
254 let a_side_effect = A::may_have_side_effect();
255 let b_side_effect = B::may_have_side_effect();
256 if a_side_effect || b_side_effect {
257 let sz_a = self.a.size();
258 let sz_b = self.b.size();
259 // Adjust a, b to equal length, make sure that only the first call
260 // of `next_back` does this, otherwise we will break the restriction
261 // on calls to `self.next_back()` after calling `get_unchecked()`.
263 let sz_a = self.a.size();
264 if a_side_effect && sz_a > self.len {
265 for _ in 0..sz_a - cmp::max(self.len, self.index) {
269 let sz_b = self.b.size();
270 if b_side_effect && sz_b > self.len {
271 for _ in 0..sz_b - self.len {
277 if self.index < self.len {
280 // SAFETY: `i` is smaller than the previous value of `self.len`,
281 // which is also smaller than or equal to `self.a.len()` and `self.b.len()`
283 Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i)))
291 unsafe fn get_unchecked(&mut self, idx: usize) -> <Self as Iterator>::Item {
292 // SAFETY: the caller must uphold the contract for
293 // `Iterator::__iterator_get_unchecked`.
294 unsafe { (self.a.__iterator_get_unchecked(idx), self.b.__iterator_get_unchecked(idx)) }
298 #[stable(feature = "rust1", since = "1.0.0")]
299 impl<A, B> ExactSizeIterator for Zip<A, B>
301 A: ExactSizeIterator,
302 B: ExactSizeIterator,
307 #[unstable(feature = "trusted_random_access", issue = "none")]
308 unsafe impl<A, B> TrustedRandomAccess for Zip<A, B>
310 A: TrustedRandomAccess,
311 B: TrustedRandomAccess,
313 fn may_have_side_effect() -> bool {
314 A::may_have_side_effect() || B::may_have_side_effect()
318 #[stable(feature = "fused", since = "1.26.0")]
319 impl<A, B> FusedIterator for Zip<A, B>
326 #[unstable(feature = "trusted_len", issue = "37572")]
327 unsafe impl<A, B> TrustedLen for Zip<A, B>
334 // Arbitrarily selects the left side of the zip iteration as extractable "source"
335 // it would require negative trait bounds to be able to try both
336 #[unstable(issue = "none", feature = "inplace_iteration")]
337 unsafe impl<S, A, B> SourceIter for Zip<A, B>
339 A: SourceIter<Source = S>,
346 unsafe fn as_inner(&mut self) -> &mut S {
347 // SAFETY: unsafe function forwarding to unsafe function with the same requirements
348 unsafe { SourceIter::as_inner(&mut self.a) }
352 #[unstable(issue = "none", feature = "inplace_iteration")]
353 // Limited to Item: Copy since interaction between Zip's use of TrustedRandomAccess
354 // and Drop implementation of the source is unclear.
356 // An additional method returning the number of times the source has been logically advanced
357 // (without calling next()) would be needed to properly drop the remainder of the source.
358 unsafe impl<A: InPlaceIterable, B: Iterator> InPlaceIterable for Zip<A, B> where A::Item: Copy {}
360 #[stable(feature = "rust1", since = "1.0.0")]
361 impl<A: Debug, B: Debug> Debug for Zip<A, B> {
362 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
368 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
371 impl<A: Debug, B: Debug> ZipFmt<A, B> for Zip<A, B> {
372 default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
373 f.debug_struct("Zip").field("a", &self.a).field("b", &self.b).finish()
377 impl<A: Debug + TrustedRandomAccess, B: Debug + TrustedRandomAccess> ZipFmt<A, B> for Zip<A, B> {
378 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
379 // It's *not safe* to call fmt on the contained iterators, since once
380 // we start iterating they're in strange, potentially unsafe, states.
381 f.debug_struct("Zip").finish()
385 /// An iterator whose items are random-accessible efficiently
389 /// The iterator's `size_hint` must be exact and cheap to call.
391 /// `size` may not be overridden.
393 /// `<Self as Iterator>::__iterator_get_unchecked` must be safe to call
394 /// provided the following conditions are met.
396 /// 1. `0 <= idx` and `idx < self.size()`.
397 /// 2. If `self: !Clone`, then `get_unchecked` is never called with the same
398 /// index on `self` more than once.
399 /// 3. After `self.get_unchecked(idx)` has been called then `next_back` will
400 /// only be called at most `self.size() - idx - 1` times.
401 /// 4. After `get_unchecked` is called, then only the following methods will be
402 /// called on `self`:
403 /// * `std::clone::Clone::clone`
404 /// * `std::iter::Iterator::size_hint()`
405 /// * `std::iter::Iterator::next_back()`
406 /// * `std::iter::Iterator::__iterator_get_unchecked()`
407 /// * `std::iter::TrustedRandomAccess::size()`
409 /// Further, given that these conditions are met, it must guarantee that:
411 /// * It does not change the value returned from `size_hint`
412 /// * It must be safe to call the methods listed above on `self` after calling
413 /// `get_unchecked`, assuming that the required traits are implemented.
414 /// * It must also be safe to drop `self` after calling `get_unchecked`.
416 #[unstable(feature = "trusted_random_access", issue = "none")]
417 #[rustc_specialization_trait]
418 pub unsafe trait TrustedRandomAccess: Sized {
419 // Convenience method.
420 fn size(&self) -> usize
426 /// Returns `true` if getting an iterator element may have
427 /// side effects. Remember to take inner iterators into account.
428 fn may_have_side_effect() -> bool;
431 /// Like `Iterator::__iterator_get_unchecked`, but doesn't require the compiler to
432 /// know that `U: TrustedRandomAccess`.
436 /// Same requirements calling `get_unchecked` directly.
438 pub(in crate::iter::adapters) unsafe fn try_get_unchecked<I>(it: &mut I, idx: usize) -> I::Item
442 // SAFETY: the caller must uphold the contract for
443 // `Iterator::__iterator_get_unchecked`.
444 unsafe { it.try_get_unchecked(idx) }
447 unsafe trait SpecTrustedRandomAccess: Iterator {
448 /// If `Self: TrustedRandomAccess`, it must be safe to call a
449 /// `Iterator::__iterator_get_unchecked(self, index)`.
450 unsafe fn try_get_unchecked(&mut self, index: usize) -> Self::Item;
453 unsafe impl<I: Iterator> SpecTrustedRandomAccess for I {
454 default unsafe fn try_get_unchecked(&mut self, _: usize) -> Self::Item {
455 panic!("Should only be called on TrustedRandomAccess iterators");
459 unsafe impl<I: Iterator + TrustedRandomAccess> SpecTrustedRandomAccess for I {
460 unsafe fn try_get_unchecked(&mut self, index: usize) -> Self::Item {
461 // SAFETY: the caller must uphold the contract for
462 // `Iterator::__iterator_get_unchecked`.
463 unsafe { self.__iterator_get_unchecked(index) }