summaryrefslogtreecommitdiffstats
path: root/third_party/rust/itertools/src/zip_eq_impl.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/itertools/src/zip_eq_impl.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/itertools/src/zip_eq_impl.rs')
-rw-r--r--third_party/rust/itertools/src/zip_eq_impl.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/third_party/rust/itertools/src/zip_eq_impl.rs b/third_party/rust/itertools/src/zip_eq_impl.rs
new file mode 100644
index 0000000000..a079b326a4
--- /dev/null
+++ b/third_party/rust/itertools/src/zip_eq_impl.rs
@@ -0,0 +1,60 @@
+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
+{}