tea_core/vec_core/iter.rs
1use std::marker::PhantomData;
2
3use crate::prelude::*;
4
5/// A trait indicating that a type can be referenced to a Trusted and DoubleEnded iterator.
6/// A trait for types that can be iterated over with a trusted iterator.
7///
8/// This trait extends the `GetLen` trait and provides methods to create
9/// trusted iterators over the implementing type.
10///
11/// # Type Parameters
12///
13/// * `T`: The type of items yielded by the iterator.
14pub trait TIter<T>: GetLen {
15 /// Creates a trusted iterator over the items of this collection.
16 ///
17 /// # Returns
18 ///
19 /// An iterator that implements the `TIterator` trait, yielding items of type `T`.
20 fn titer(&self) -> impl TIterator<Item = T> + '_;
21
22 /// Maps each item in the collection using the provided function.
23 ///
24 /// This method creates a new iterator that applies the given function to
25 /// each item yielded by the original iterator.
26 ///
27 /// # Arguments
28 ///
29 /// * `f`: A function that takes an item of type `T` and returns an item of type `U`.
30 ///
31 /// # Returns
32 ///
33 /// An iterator that yields the mapped items.
34 ///
35 /// # Type Parameters
36 ///
37 /// * `'a`: The lifetime of the reference to `self`.
38 /// * `U`: The type of items yielded by the new iterator.
39 /// * `F`: The type of the mapping function.
40 #[inline]
41 fn map<'a, U, F>(&'a self, f: F) -> impl TIterator<Item = U>
42 where
43 F: FnMut(T) -> U,
44 T: 'a,
45 {
46 self.titer().map(f)
47 }
48}
49
50/// A trait indicating that a type can be converted into a Trusted and DoubleEnded iterator.
51pub trait IntoTIter: IntoIterator
52where
53 Self::IntoIter: TIterator,
54{
55 fn into_titer(self) -> Self::IntoIter
56 where
57 Self: Sized;
58}
59
60impl<I: IntoIterator + GetLen> IntoTIter for I
61where
62 Self::IntoIter: TIterator,
63{
64 #[inline]
65 fn into_titer(self) -> Self::IntoIter {
66 self.into_iter()
67 }
68}
69/// An iterator adapter that wraps a `Vec1View` and converts its items to `Option` types.
70///
71/// This struct provides a way to iterate over a `Vec1View` while converting each item
72/// to an `Option` type. It's particularly useful when working with types that implement
73/// the `IsNone` trait, allowing for a uniform representation of potentially absent values.
74///
75/// # Type Parameters
76///
77/// * `'a`: The lifetime of the reference to the underlying `Vec1View`.
78/// * `V`: The type of the underlying `Vec1View`.
79/// * `T`: The item type of the `Vec1View`.
80///
81/// # Fields
82///
83/// * `view`: A reference to the underlying `Vec1View`.
84/// * `item`: A `PhantomData` to carry the item type `T`.
85pub struct OptIter<'a, V: Vec1View<T>, T> {
86 pub view: &'a V,
87 pub item: PhantomData<T>,
88}
89
90impl<V: Vec1View<T>, T> GetLen for OptIter<'_, V, T> {
91 #[inline]
92 fn len(&self) -> usize {
93 self.view.len()
94 }
95}
96
97impl<V: Vec1View<T>, T: IsNone> TIter<Option<<T as IsNone>::Inner>> for OptIter<'_, V, T> {
98 #[inline]
99 fn titer(&self) -> impl TIterator<Item = Option<<T as IsNone>::Inner>> {
100 self.view.titer().map(|v| v.to_opt())
101 }
102}
103
104impl<'a, T: IsNone + 'a, V: Vec1View<T>> Vec1View<Option<T::Inner>> for OptIter<'a, V, T>
105where
106 for<'b> V::SliceOutput<'b>: TIter<T>,
107{
108 type SliceOutput<'b> = Vec<Option<T::Inner>> where Self: 'b;
109
110 #[inline]
111 fn slice<'b>(&'b self, start: usize, end: usize) -> TResult<Self::SliceOutput<'b>>
112 where
113 T: 'b,
114 {
115 Ok(self
116 .view
117 .slice(start, end)?
118 .titer()
119 .map(|v| v.to_opt())
120 .collect_trusted_to_vec())
121 }
122
123 #[inline]
124 fn get_backend_name(&self) -> &'static str {
125 self.view.get_backend_name()
126 }
127
128 #[inline]
129 unsafe fn uget(&self, index: usize) -> Option<T::Inner> {
130 self.view.uget(index).to_opt()
131 }
132}
133
134impl<'a, 'b, V: Vec1View<T>, T: IsNone> IntoIterator for &'b OptIter<'a, V, T> {
135 type Item = Option<T::Inner>;
136 type IntoIter = Box<dyn TrustedLen<Item = Option<T::Inner>> + 'b>;
137 #[inline]
138 fn into_iter(self) -> Self::IntoIter {
139 Box::new(self.titer())
140 }
141}