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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
pub use self::sample_on::SampleOn;
pub mod sample_on {
pub trait SampleOn: Sized + Iterator {
#[inline]
fn sample_on<O: Iterator>(self, other: O) -> Items<Self, O> {
Items { sample: self, on: other, last_sample: None, is_infinite: false }
}
}
pub struct Items<A, B> where A: Iterator, B: Iterator {
sample: A,
on: B,
last_sample: Option<A::Item>,
is_infinite: bool,
}
impl<A, B> Items<A, B> where A: Iterator, B: Iterator {
#[inline]
pub fn infinite(self) -> Items<A, B> {
Items { is_infinite: true, ..self }
}
}
impl<A, B> Iterator for Items<A, B>
where
A: Iterator,
B: Iterator,
A::Item: Clone
{
type Item = A::Item;
fn next(&mut self) -> Option<A::Item> {
while let None = self.on.next() {}
match self.sample.next() {
None => if self.is_infinite { self.last_sample.clone() } else { None },
Some(sample) => {
self.last_sample = Some(sample.clone());
Some(sample)
},
}
}
}
}