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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// The <code>impl [LendingIterator]</code> returned by [`.filter_map()`][
/// LendingIterator::filter_map()].
pub
struct FilterMap<I, F, NewItemType>
where
    I : LendingIterator,
    NewItemType : HKT,
    for<'any>
        F : FnMut(
            [&'any I; 0],
            Item<'any, I>,
        ) -> Option<A!(NewItemType<'any>)>
    ,
{
    pub(in crate)
    iter: I,

    pub(in crate)
    map: F,

    pub(in crate)
    _phantom_ty: ::core::marker::PhantomData<fn() -> NewItemType>,
}

#[gat]
impl<I, NewItemType, F> LendingIterator
    for FilterMap<I, F, NewItemType>
where
    I : LendingIterator,
    NewItemType : HKT,
    for<'any>
        F : FnMut(
            [&'any I; 0],
            Item<'any, I>,
        ) -> Option<A!(NewItemType<'any>)>
    ,
{
    type Item<'next>
    where
        Self : 'next,
    =
        A!(NewItemType<'next>)
    ;

    fn next (
        self: &'_ mut FilterMap<I, F, NewItemType>,
    ) -> Option<A!(NewItemType<'_>)>
    {
        self.iter.next().and_then(|item| (self.map)([], item))
    }
}

/// The <code>impl [LendingIterator]</code> returned by
/// [`.filter_map_into_iter()`][LendingIterator::filter_map_into_iter()].
pub
struct FilterMapIntoIter<I, F>
(
    pub(in crate) I,
    pub(in crate) F,
)
where
    I : LendingIterator,
    for<'any>
        F : crate::utils::FnMut<Item<'any, I>>
    ,
;

impl<I, F, R>
    Iterator
for
    FilterMapIntoIter<I, F>
where
    I : LendingIterator,
    F : FnMut(Item<'_, I>) -> Option<R>,
{
    type Item = R;

    fn next (
        self: &'_ mut FilterMapIntoIter<I, F>,
    ) -> Option<R>
    {
        self.0.next().and_then(&mut self.1)
    }
}