Sha256: 1c8126e015e162a4e41451b155d76ded7ab325d0da9e1197ba410322ca966412

Contents?: true

Size: 1.53 KB

Versions: 13

Compression:

Stored size: 1.53 KB

Contents

use super::size_hint;

/// An iterator which iterates two other iterators simultaneously
///
/// See [`.zip_eq()`](crate::Itertools::zip_eq) for more information.
#[derive(Clone, Debug)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct ZipEq<I, J> {
    a: I,
    b: J,
}

/// Iterate `i` and `j` in lock step.
///
/// **Panics** if the iterators are not of the same length.
///
/// [`IntoIterator`] enabled version of [`Itertools::zip_eq`](crate::Itertools::zip_eq).
///
/// ```
/// use itertools::zip_eq;
///
/// let data = [1, 2, 3, 4, 5];
/// for (a, b) in zip_eq(&data[..data.len() - 1], &data[1..]) {
///     /* loop body */
/// }
/// ```
pub fn zip_eq<I, J>(i: I, j: J) -> ZipEq<I::IntoIter, J::IntoIter>
where
    I: IntoIterator,
    J: IntoIterator,
{
    ZipEq {
        a: i.into_iter(),
        b: j.into_iter(),
    }
}

impl<I, J> Iterator for ZipEq<I, J>
where
    I: Iterator,
    J: Iterator,
{
    type Item = (I::Item, J::Item);

    fn next(&mut self) -> Option<Self::Item> {
        match (self.a.next(), self.b.next()) {
            (None, None) => None,
            (Some(a), Some(b)) => Some((a, b)),
            (None, Some(_)) | (Some(_), None) => {
                panic!("itertools: .zip_eq() reached end of one iterator before the other")
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        size_hint::min(self.a.size_hint(), self.b.size_hint())
    }
}

impl<I, J> ExactSizeIterator for ZipEq<I, J>
where
    I: ExactSizeIterator,
    J: ExactSizeIterator,
{
}

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-28.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-27.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-26.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-25.0.2 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-25.0.1 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-25.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-24.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-23.0.2 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-22.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-21.0.1 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-20.0.2 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs
wasmtime-20.0.0 ./ext/cargo-vendor/itertools-0.12.1/src/zip_eq_impl.rs