Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use core::any::type_name;
9use core::cmp;
10
11pub trait IteratorExt {
12 fn zip_eq<U>(self, other: U) -> ZipEq<Self, <U as IntoIterator>::IntoIter>
13 where
14 Self: Sized,
15 U: IntoIterator;
16}
17
18impl<I> IteratorExt for I
19where
20 I: Iterator,
21{
22 fn zip_eq<U>(self, other: U) -> ZipEq<Self, <U as IntoIterator>::IntoIter>
23 where
24 Self: Sized,
25 U: IntoIterator,
26 {
27 ZipEq {
28 a: self,
29 b: other.into_iter(),
30 }
31 }
32}
33
34/// like Iterator::zip but panics if one iterator ends before
35/// the other. The `param_predicate` is required to select exactly as many
36/// elements of `params` as there are elements in `arguments`.
37pub struct ZipEq<A, B> {
38 a: A,
39 b: B,
40}
41
42impl<A, B> Iterator for ZipEq<A, B>
43where
44 A: Iterator,
45 B: Iterator,
46{
47 type Item = (A::Item, B::Item);
48
49 fn next(&mut self) -> Option<Self::Item> {
50 match (self.a.next(), self.b.next()) {
51 (Some(a), Some(b)) => Some((a, b)),
52 (None, None) => None,
53 (None, _) => panic!(
54 "iterators had different lengths. {} was shorter than {}",
55 type_name::<A>(),
56 type_name::<B>()
57 ),
58 (_, None) => panic!(
59 "iterators had different lengths. {} was shorter than {}",
60 type_name::<B>(),
61 type_name::<A>()
62 ),
63 }
64 }
65
66 fn size_hint(&self) -> (usize, Option<usize>) {
67 let (a_min, a_max) = self.a.size_hint();
68 let (b_min, b_max) = self.a.size_hint();
69 (
70 cmp::min(a_min, b_min),
71 a_max
72 .and_then(|a| Some((a, b_max?)))
73 .map(|(a, b)| cmp::min(a, b)),
74 )
75 }
76}
77
78impl<A, B> ExactSizeIterator for ZipEq<A, B>
79where
80 A: ExactSizeIterator,
81 B: ExactSizeIterator,
82{
83 fn len(&self) -> usize {
84 debug_assert_eq!(self.a.len(), self.b.len());
85 self.a.len()
86 }
87}