1use super::{for_both, Either, Left, Right};
2use core::iter;
3
4macro_rules! wrap_either {
5 ($value:expr => $( $tail:tt )*) => {
6 match $value {
7 Left(inner) => inner.map(Left) $($tail)*,
8 Right(inner) => inner.map(Right) $($tail)*,
9 }
10 };
11}
12
13#[derive(Clone, Debug)]
19pub struct IterEither<L, R> {
20 inner: Either<L, R>,
21}
22
23impl<L, R> IterEither<L, R> {
24 pub(crate) fn new(inner: Either<L, R>) -> Self {
25 IterEither { inner }
26 }
27}
28
29impl<L, R, A> Extend<A> for Either<L, R>
30where
31 L: Extend<A>,
32 R: Extend<A>,
33{
34 fn extend<T>(&mut self, iter: T)
35 where
36 T: IntoIterator<Item = A>,
37 {
38 for_both!(self, inner => inner.extend(iter))
39 }
40}
41
42impl<L, R> Iterator for Either<L, R>
44where
45 L: Iterator,
46 R: Iterator<Item = L::Item>,
47{
48 type Item = L::Item;
49
50 fn next(&mut self) -> Option<Self::Item> {
51 for_both!(self, inner => inner.next())
52 }
53
54 fn size_hint(&self) -> (usize, Option<usize>) {
55 for_both!(self, inner => inner.size_hint())
56 }
57
58 fn fold<Acc, G>(self, init: Acc, f: G) -> Acc
59 where
60 G: FnMut(Acc, Self::Item) -> Acc,
61 {
62 for_both!(self, inner => inner.fold(init, f))
63 }
64
65 fn for_each<F>(self, f: F)
66 where
67 F: FnMut(Self::Item),
68 {
69 for_both!(self, inner => inner.for_each(f))
70 }
71
72 fn count(self) -> usize {
73 for_both!(self, inner => inner.count())
74 }
75
76 fn last(self) -> Option<Self::Item> {
77 for_both!(self, inner => inner.last())
78 }
79
80 fn nth(&mut self, n: usize) -> Option<Self::Item> {
81 for_both!(self, inner => inner.nth(n))
82 }
83
84 fn collect<B>(self) -> B
85 where
86 B: iter::FromIterator<Self::Item>,
87 {
88 for_both!(self, inner => inner.collect())
89 }
90
91 fn partition<B, F>(self, f: F) -> (B, B)
92 where
93 B: Default + Extend<Self::Item>,
94 F: FnMut(&Self::Item) -> bool,
95 {
96 for_both!(self, inner => inner.partition(f))
97 }
98
99 fn all<F>(&mut self, f: F) -> bool
100 where
101 F: FnMut(Self::Item) -> bool,
102 {
103 for_both!(self, inner => inner.all(f))
104 }
105
106 fn any<F>(&mut self, f: F) -> bool
107 where
108 F: FnMut(Self::Item) -> bool,
109 {
110 for_both!(self, inner => inner.any(f))
111 }
112
113 fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
114 where
115 P: FnMut(&Self::Item) -> bool,
116 {
117 for_both!(self, inner => inner.find(predicate))
118 }
119
120 fn find_map<B, F>(&mut self, f: F) -> Option<B>
121 where
122 F: FnMut(Self::Item) -> Option<B>,
123 {
124 for_both!(self, inner => inner.find_map(f))
125 }
126
127 fn position<P>(&mut self, predicate: P) -> Option<usize>
128 where
129 P: FnMut(Self::Item) -> bool,
130 {
131 for_both!(self, inner => inner.position(predicate))
132 }
133}
134
135impl<L, R> DoubleEndedIterator for Either<L, R>
136where
137 L: DoubleEndedIterator,
138 R: DoubleEndedIterator<Item = L::Item>,
139{
140 fn next_back(&mut self) -> Option<Self::Item> {
141 for_both!(self, inner => inner.next_back())
142 }
143
144 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
145 for_both!(self, inner => inner.nth_back(n))
146 }
147
148 fn rfold<Acc, G>(self, init: Acc, f: G) -> Acc
149 where
150 G: FnMut(Acc, Self::Item) -> Acc,
151 {
152 for_both!(self, inner => inner.rfold(init, f))
153 }
154
155 fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
156 where
157 P: FnMut(&Self::Item) -> bool,
158 {
159 for_both!(self, inner => inner.rfind(predicate))
160 }
161}
162
163impl<L, R> ExactSizeIterator for Either<L, R>
164where
165 L: ExactSizeIterator,
166 R: ExactSizeIterator<Item = L::Item>,
167{
168 fn len(&self) -> usize {
169 for_both!(self, inner => inner.len())
170 }
171}
172
173impl<L, R> iter::FusedIterator for Either<L, R>
174where
175 L: iter::FusedIterator,
176 R: iter::FusedIterator<Item = L::Item>,
177{
178}
179
180impl<L, R> Iterator for IterEither<L, R>
181where
182 L: Iterator,
183 R: Iterator,
184{
185 type Item = Either<L::Item, R::Item>;
186
187 fn next(&mut self) -> Option<Self::Item> {
188 Some(map_either!(self.inner, ref mut inner => inner.next()?))
189 }
190
191 fn size_hint(&self) -> (usize, Option<usize>) {
192 for_both!(self.inner, ref inner => inner.size_hint())
193 }
194
195 fn fold<Acc, G>(self, init: Acc, f: G) -> Acc
196 where
197 G: FnMut(Acc, Self::Item) -> Acc,
198 {
199 wrap_either!(self.inner => .fold(init, f))
200 }
201
202 fn for_each<F>(self, f: F)
203 where
204 F: FnMut(Self::Item),
205 {
206 wrap_either!(self.inner => .for_each(f))
207 }
208
209 fn count(self) -> usize {
210 for_both!(self.inner, inner => inner.count())
211 }
212
213 fn last(self) -> Option<Self::Item> {
214 Some(map_either!(self.inner, inner => inner.last()?))
215 }
216
217 fn nth(&mut self, n: usize) -> Option<Self::Item> {
218 Some(map_either!(self.inner, ref mut inner => inner.nth(n)?))
219 }
220
221 fn collect<B>(self) -> B
222 where
223 B: iter::FromIterator<Self::Item>,
224 {
225 wrap_either!(self.inner => .collect())
226 }
227
228 fn partition<B, F>(self, f: F) -> (B, B)
229 where
230 B: Default + Extend<Self::Item>,
231 F: FnMut(&Self::Item) -> bool,
232 {
233 wrap_either!(self.inner => .partition(f))
234 }
235
236 fn all<F>(&mut self, f: F) -> bool
237 where
238 F: FnMut(Self::Item) -> bool,
239 {
240 wrap_either!(&mut self.inner => .all(f))
241 }
242
243 fn any<F>(&mut self, f: F) -> bool
244 where
245 F: FnMut(Self::Item) -> bool,
246 {
247 wrap_either!(&mut self.inner => .any(f))
248 }
249
250 fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
251 where
252 P: FnMut(&Self::Item) -> bool,
253 {
254 wrap_either!(&mut self.inner => .find(predicate))
255 }
256
257 fn find_map<B, F>(&mut self, f: F) -> Option<B>
258 where
259 F: FnMut(Self::Item) -> Option<B>,
260 {
261 wrap_either!(&mut self.inner => .find_map(f))
262 }
263
264 fn position<P>(&mut self, predicate: P) -> Option<usize>
265 where
266 P: FnMut(Self::Item) -> bool,
267 {
268 wrap_either!(&mut self.inner => .position(predicate))
269 }
270}
271
272impl<L, R> DoubleEndedIterator for IterEither<L, R>
273where
274 L: DoubleEndedIterator,
275 R: DoubleEndedIterator,
276{
277 fn next_back(&mut self) -> Option<Self::Item> {
278 Some(map_either!(self.inner, ref mut inner => inner.next_back()?))
279 }
280
281 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
282 Some(map_either!(self.inner, ref mut inner => inner.nth_back(n)?))
283 }
284
285 fn rfold<Acc, G>(self, init: Acc, f: G) -> Acc
286 where
287 G: FnMut(Acc, Self::Item) -> Acc,
288 {
289 wrap_either!(self.inner => .rfold(init, f))
290 }
291
292 fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
293 where
294 P: FnMut(&Self::Item) -> bool,
295 {
296 wrap_either!(&mut self.inner => .rfind(predicate))
297 }
298}
299
300impl<L, R> ExactSizeIterator for IterEither<L, R>
301where
302 L: ExactSizeIterator,
303 R: ExactSizeIterator,
304{
305 fn len(&self) -> usize {
306 for_both!(self.inner, ref inner => inner.len())
307 }
308}
309
310impl<L, R> iter::FusedIterator for IterEither<L, R>
311where
312 L: iter::FusedIterator,
313 R: iter::FusedIterator,
314{
315}