orx_iterable/transformations/
chained.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use crate::{Collection, CollectionMut, Iterable};
use core::marker::PhantomData;
use orx_self_or::SoM;

/// An iterable created by chaining two iterables.
pub struct Chained<I1, I2>
where
    I1: Iterable,
    I2: Iterable<Item = I1::Item>,
{
    pub(crate) it1: I1,
    pub(crate) it2: I2,
}

impl<I1, I2> Iterable for Chained<I1, I2>
where
    I1: Iterable,
    I2: Iterable<Item = I1::Item>,
{
    type Item = I1::Item;

    type Iter = core::iter::Chain<I1::Iter, I2::Iter>;

    fn iter(&self) -> Self::Iter {
        self.it1.iter().chain(self.it2.iter())
    }
}

// col

/// An iterable collection created by chaining two iterable collections.
pub struct ChainedCol<I1, I2, E1, E2>
where
    I1: Collection,
    I2: Collection<Item = I1::Item>,
    E1: SoM<I1>,
    E2: SoM<I2>,
{
    pub(crate) it1: E1,
    pub(crate) it2: E2,
    pub(crate) phantom: PhantomData<(I1, I2)>,
}

impl<'a, I1, I2, E1, E2> Iterable for &'a ChainedCol<I1, I2, E1, E2>
where
    I1: Collection,
    I2: Collection<Item = I1::Item>,
    E1: SoM<I1>,
    E2: SoM<I2>,
{
    type Item = &'a I1::Item;

    type Iter = core::iter::Chain<
        <I1::Iterable<'a> as Iterable>::Iter,
        <I2::Iterable<'a> as Iterable>::Iter,
    >;

    fn iter(&self) -> Self::Iter {
        self.it1.get_ref().iter().chain(self.it2.get_ref().iter())
    }
}

impl<I1, I2, E1, E2> Collection for ChainedCol<I1, I2, E1, E2>
where
    I1: Collection,
    I2: Collection<Item = I1::Item>,
    E1: SoM<I1>,
    E2: SoM<I2>,
{
    type Item = I1::Item;

    type Iterable<'i>
        = &'i Self
    where
        Self: 'i;

    fn as_iterable(&self) -> Self::Iterable<'_> {
        self
    }
}

impl<I1, I2, E1, E2> CollectionMut for ChainedCol<I1, I2, E1, E2>
where
    I1: CollectionMut,
    I2: CollectionMut<Item = I1::Item>,
    E1: SoM<I1>,
    E2: SoM<I2>,
{
    type IterMut<'i>
        = core::iter::Chain<I1::IterMut<'i>, I2::IterMut<'i>>
    where
        Self: 'i;

    fn iter_mut(&mut self) -> Self::IterMut<'_> {
        self.it1
            .get_mut()
            .iter_mut()
            .chain(self.it2.get_mut().iter_mut())
    }
}