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
use super::{CurrentId, IntoIterator, Shiperator};

/// Shiperator filtering all components with `pred`.
pub struct Filter<I, P> {
    iter: I,
    pred: P,
}

impl<I, P> Filter<I, P> {
    pub(super) fn new(iter: I, pred: P) -> Self {
        Filter { iter, pred }
    }
}

impl<I: Shiperator, P> Shiperator for Filter<I, P>
where
    P: FnMut(&I::Item) -> bool,
{
    type Item = I::Item;

    fn first_pass(&mut self) -> Option<Self::Item> {
        while let Some(item) = self.iter.first_pass() {
            if (self.pred)(&item) {
                return Some(item);
            }
        }
        None
    }
    fn post_process(&mut self) {
        self.iter.post_process()
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, self.iter.size_hint().1)
    }
}

impl<I: CurrentId, P> CurrentId for Filter<I, P>
where
    P: FnMut(&I::Item) -> bool,
{
    type Id = I::Id;

    unsafe fn current_id(&self) -> Self::Id {
        self.iter.current_id()
    }
}

impl<I: Shiperator, P> core::iter::IntoIterator for Filter<I, P>
where
    P: FnMut(&I::Item) -> bool,
{
    type IntoIter = IntoIterator<Self>;
    type Item = <Self as Shiperator>::Item;
    fn into_iter(self) -> Self::IntoIter {
        IntoIterator(self)
    }
}