1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct Alternate<A, B> {
a: A,
b: B,
odd: bool,
done: bool,
}
impl<
A: Iterator<Item = T>,
B: Iterator<Item = T>,
T
> Alternate<A, B> {
pub(crate) fn new(a: A, b: B) -> Self {
Self {
a,
b,
odd: false,
done: false,
}
}
}
impl<A: Iterator<Item = T>, B: Iterator<Item = T>, T> Iterator for Alternate<A, B> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.done { return None }
let item = if self.odd {
self.b.next()
} else {
self.a.next()
};
if item.is_none() {
self.done = true;
}
self.odd = !self.odd;
item
}
}