1#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
5pub struct ZipEq<A, B> {
6 a: A,
7 b: B,
8}
9
10pub fn zip_eq<A, AIter, B, BIter, Error>(
14 a: A,
15 b: B,
16 err: Error,
17) -> Result<ZipEq<A::IntoIter, B::IntoIter>, Error>
18where
19 A: IntoIterator<IntoIter = AIter>,
20 AIter: ExactSizeIterator,
21 B: IntoIterator<IntoIter = BIter>,
22 BIter: ExactSizeIterator,
23{
24 let a_iter = a.into_iter();
25 let b_iter = b.into_iter();
26 match a_iter.len() == b_iter.len() {
27 true => Ok(ZipEq {
28 a: a_iter,
29 b: b_iter,
30 }),
31 false => Err(err),
32 }
33}
34
35impl<A, B> Iterator for ZipEq<A, B>
36where
37 A: ExactSizeIterator, B: ExactSizeIterator,
39{
40 type Item = (A::Item, B::Item);
41
42 fn next(&mut self) -> Option<Self::Item> {
43 match (self.a.next(), self.b.next()) {
44 (Some(a), Some(b)) => Some((a, b)),
45 (None, None) => None,
46 _ => unreachable!("The iterators must have the same length."),
47 }
48 }
49
50 fn size_hint(&self) -> (usize, Option<usize>) {
51 debug_assert_eq!(self.a.size_hint(), self.b.size_hint());
54 self.a.size_hint()
55 }
56}
57
58impl<A, B> ExactSizeIterator for ZipEq<A, B>
59where
60 A: ExactSizeIterator,
61 B: ExactSizeIterator,
62{
63}
64
65#[cfg(test)]
66mod tests {
67 use alloc::vec;
68 use alloc::vec::Vec;
69
70 use super::*;
71
72 #[test]
73 fn test_zip_eq_success() {
74 let a = [1, 2, 3];
75 let b = ['a', 'b', 'c'];
76
77 let zipped = zip_eq(a, b, "length mismatch").unwrap();
79
80 let result: Vec<_> = zipped.collect();
81
82 assert_eq!(result, vec![(1, 'a'), (2, 'b'), (3, 'c')]);
84 }
85
86 #[test]
87 fn test_zip_eq_length_mismatch() {
88 let a = [1, 2];
89 let b = ['x', 'y', 'z'];
90
91 match zip_eq(a, b, "oops") {
93 Err(e) => assert_eq!(e, "oops"),
94 Ok(_) => panic!("expected error due to mismatched lengths"),
95 }
96 }
97
98 #[test]
99 fn test_zip_eq_empty_iterators() {
100 let a: [i32; 0] = [];
101 let b: [char; 0] = [];
102
103 let zipped = zip_eq(a, b, "mismatch").unwrap();
105
106 let result: Vec<_> = zipped.collect();
107
108 assert!(result.is_empty());
110 }
111
112 #[test]
113 fn test_zip_eq_size_hint() {
114 let a = [10, 20];
115 let b = [100, 200];
116
117 let zipped = zip_eq(a, b, "bad").unwrap();
118
119 assert_eq!(zipped.size_hint(), (2, Some(2)));
121 }
122
123 #[test]
124 fn test_zip_eq_unreachable_case() {
125 let a = [1, 2];
126 let b = [3, 4];
127
128 let mut zipped = zip_eq(a, b, "fail").unwrap();
129
130 assert_eq!(zipped.next(), Some((1, 3)));
132 assert_eq!(zipped.next(), Some((2, 4)));
133 assert_eq!(zipped.next(), None);
134 }
135}