orx_iterable/transformations/
chained.rs

1use crate::{Collection, CollectionMut, Iterable};
2use core::marker::PhantomData;
3use orx_self_or::SoM;
4
5/// An iterable created by chaining two iterables.
6pub struct Chained<I1, I2>
7where
8    I1: Iterable,
9    I2: Iterable<Item = I1::Item>,
10{
11    pub(crate) it1: I1,
12    pub(crate) it2: I2,
13}
14
15impl<I1, I2> Iterable for Chained<I1, I2>
16where
17    I1: Iterable,
18    I2: Iterable<Item = I1::Item>,
19{
20    type Item = I1::Item;
21
22    type Iter = core::iter::Chain<I1::Iter, I2::Iter>;
23
24    fn iter(&self) -> Self::Iter {
25        self.it1.iter().chain(self.it2.iter())
26    }
27}
28
29// col
30
31/// An iterable collection created by chaining two iterable collections.
32pub struct ChainedCol<I1, I2, E1, E2>
33where
34    I1: Collection,
35    I2: Collection<Item = I1::Item>,
36    E1: SoM<I1>,
37    E2: SoM<I2>,
38{
39    pub(crate) it1: E1,
40    pub(crate) it2: E2,
41    pub(crate) phantom: PhantomData<(I1, I2)>,
42}
43
44impl<'a, I1, I2, E1, E2> Iterable for &'a ChainedCol<I1, I2, E1, E2>
45where
46    I1: Collection,
47    I2: Collection<Item = I1::Item>,
48    E1: SoM<I1>,
49    E2: SoM<I2>,
50{
51    type Item = &'a I1::Item;
52
53    type Iter = core::iter::Chain<
54        <I1::Iterable<'a> as Iterable>::Iter,
55        <I2::Iterable<'a> as Iterable>::Iter,
56    >;
57
58    fn iter(&self) -> Self::Iter {
59        self.it1.get_ref().iter().chain(self.it2.get_ref().iter())
60    }
61}
62
63impl<I1, I2, E1, E2> Collection for ChainedCol<I1, I2, E1, E2>
64where
65    I1: Collection,
66    I2: Collection<Item = I1::Item>,
67    E1: SoM<I1>,
68    E2: SoM<I2>,
69{
70    type Item = I1::Item;
71
72    type Iterable<'i>
73        = &'i Self
74    where
75        Self: 'i;
76
77    fn as_iterable(&self) -> Self::Iterable<'_> {
78        self
79    }
80}
81
82impl<I1, I2, E1, E2> CollectionMut for ChainedCol<I1, I2, E1, E2>
83where
84    I1: CollectionMut,
85    I2: CollectionMut<Item = I1::Item>,
86    E1: SoM<I1>,
87    E2: SoM<I2>,
88{
89    type IterMut<'i>
90        = core::iter::Chain<I1::IterMut<'i>, I2::IterMut<'i>>
91    where
92        Self: 'i;
93
94    fn iter_mut(&mut self) -> Self::IterMut<'_> {
95        self.it1
96            .get_mut()
97            .iter_mut()
98            .chain(self.it2.get_mut().iter_mut())
99    }
100}