orx_iterable/transformations/
mapped_while.rs

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
use crate::Iterable;

/// Wraps an `Iterable` and creates a new `Iterable` which maps the elements of
/// the original iterable as long as the map-while condition is satisfied.
pub struct MappedWhile<I, M, U>
where
    I: Iterable,
    M: Fn(I::Item) -> Option<U> + Copy,
{
    pub(crate) it: I,
    pub(crate) map_while: M,
}

impl<I, M, U> Iterable for MappedWhile<I, M, U>
where
    I: Iterable,
    M: Fn(I::Item) -> Option<U> + Copy,
{
    type Item = U;

    type Iter = core::iter::MapWhile<I::Iter, M>;

    fn iter(&self) -> Self::Iter {
        self.it.iter().map_while(self.map_while)
    }
}