1use super::size_hint;
2
3/// An iterator which iterates two other iterators simultaneously
4///
5/// See [`.zip_eq()`](crate::Itertools::zip_eq) for more information.
6#[derive(Clone, Debug)]
7#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
8pub struct ZipEq<I, J> {
9 a: I,
10 b: J,
11}
12
13/// Iterate `i` and `j` in lock step.
14///
15/// **Panics** if the iterators are not of the same length.
16///
17/// [`IntoIterator`] enabled version of [`Itertools::zip_eq`](crate::Itertools::zip_eq).
18///
19/// ```
20/// use itertools::zip_eq;
21///
22/// let data = [1, 2, 3, 4, 5];
23/// for (a, b) in zip_eq(&data[..data.len() - 1], &data[1..]) {
24/// /* loop body */
25/// }
26/// ```
27pub fn zip_eq<I, J>(i: I, j: J) -> ZipEq<I::IntoIter, J::IntoIter>
28where
29 I: IntoIterator,
30 J: IntoIterator,
31{
32 ZipEq {
33 a: i.into_iter(),
34 b: j.into_iter(),
35 }
36}
37
38impl<I, J> Iterator for ZipEq<I, J>
39where
40 I: Iterator,
41 J: Iterator,
42{
43 type Item = (I::Item, J::Item);
44
45 fn next(&mut self) -> Option<Self::Item> {
46 match (self.a.next(), self.b.next()) {
47 (None, None) => None,
48 (Some(a: ::Item), Some(b: ::Item)) => Some((a, b)),
49 (None, Some(_)) | (Some(_), None) => {
50 panic!("itertools: .zip_eq() reached end of one iterator before the other")
51 }
52 }
53 }
54
55 fn size_hint(&self) -> (usize, Option<usize>) {
56 size_hint::min(self.a.size_hint(), self.b.size_hint())
57 }
58}
59
60impl<I, J> ExactSizeIterator for ZipEq<I, J>
61where
62 I: ExactSizeIterator,
63 J: ExactSizeIterator,
64{
65}
66