orx_iterable/transformations/
taken.rs1use crate::{Collection, CollectionMut, Iterable};
2use core::marker::PhantomData;
3use orx_self_or::SoM;
4
5pub struct Taken<I>
8where
9 I: Iterable,
10{
11 pub(crate) it: I,
12 pub(crate) n: usize,
13}
14
15impl<I> Iterable for Taken<I>
16where
17 I: Iterable,
18{
19 type Item = I::Item;
20
21 type Iter = core::iter::Take<I::Iter>;
22
23 fn iter(&self) -> Self::Iter {
24 self.it.iter().take(self.n)
25 }
26}
27
28pub struct TakenCol<I, E>
33where
34 I: Collection,
35 E: SoM<I>,
36{
37 pub(crate) it: E,
38 pub(crate) n: usize,
39 pub(crate) phantom: PhantomData<I>,
40}
41
42impl<'a, I, E> Iterable for &'a TakenCol<I, E>
43where
44 I: Collection,
45 E: SoM<I>,
46{
47 type Item = &'a I::Item;
48
49 type Iter = core::iter::Take<<I::Iterable<'a> as Iterable>::Iter>;
50
51 fn iter(&self) -> Self::Iter {
52 self.it.get_ref().iter().take(self.n)
53 }
54}
55
56impl<I, E> Collection for TakenCol<I, E>
57where
58 I: Collection,
59 E: SoM<I>,
60{
61 type Item = I::Item;
62
63 type Iterable<'i> = &'i Self
64 where
65 Self: 'i;
66
67 fn as_iterable(&self) -> Self::Iterable<'_> {
68 self
69 }
70}
71
72impl<I, E> CollectionMut for TakenCol<I, E>
73where
74 I: CollectionMut,
75 E: SoM<I>,
76{
77 type IterMut<'i> = core::iter::Take<I::IterMut<'i>>
78 where
79 Self: 'i;
80
81 fn iter_mut(&mut self) -> Self::IterMut<'_> {
82 self.it.get_mut().iter_mut().take(self.n)
83 }
84}