anon_vec/
iter.rs

1
2use std::any::Any;
3
4/// An Immutable Iterator over an Anonymously Typed Vec.
5#[derive(Copy, Clone)]
6pub struct AnonIter<T>
7where
8    T: Any + 'static,
9{
10    pub(crate) data: *const T,
11    pub(crate) curr: usize,
12    pub(crate) len: usize,
13}
14
15impl<T> AnonIter<T>
16where
17    T: Any + 'static,
18{
19    pub fn next_unchecked(&mut self) -> &'static T {
20        unsafe { &*self.data.add(self.curr - 1) }
21    }
22}
23
24
25impl<T> Iterator for AnonIter<T>
26where
27    T: Any + 'static,
28{
29    type Item = &'static T;
30
31    fn next(&mut self) -> Option<Self::Item> {
32        if self.curr == self.len {
33            None
34        } else {
35            self.curr += 1;
36            unsafe {
37                Some(&*self.data.add(self.curr - 1))
38            }
39        }
40    }
41}
42
43/// A Mutable Iterator over an Anonymously Typed Vec.
44#[derive(Copy, Clone)]
45pub struct AnonIterMut<T>
46where
47    T: Any + 'static,
48{
49    pub(crate) data: *mut T,
50    pub(crate) curr: usize,
51    pub(crate) len: usize,
52}
53
54impl<T> AnonIterMut<T>
55where
56    T: Any + 'static,
57{
58    pub fn next_unchecked(&mut self) -> &'static mut T {
59        unsafe { &mut *self.data.add(self.curr - 1) }
60    }
61}
62
63impl<T> Iterator for AnonIterMut<T>
64where
65    T: Any + 'static,
66{
67    type Item = &'static mut T;
68
69    fn next(&mut self) -> Option<Self::Item> {
70        if self.curr == self.len {
71            None
72        } else {
73            self.curr += 1;
74            unsafe {
75                Some(&mut *self.data.add(self.curr - 1))
76            }
77        }
78    }
79}
80
81#[derive(Clone)]
82pub struct AnonChain<T>
83where
84    T: Any + 'static,
85{
86    iters: Vec<AnonIter<T>>,
87}
88
89impl<T> AnonChain<T> 
90where
91    T: 'static,
92{
93    pub fn push(&mut self, iter: AnonIter<T>) {
94        self.iters.push(iter)
95    }
96
97    pub fn new() -> Self {
98        Self {
99            iters: Vec::with_capacity(16),
100        }
101    }
102
103    pub fn is_empty(&self) -> bool {
104        self.iters.is_empty()
105    }
106}
107
108impl<T> Iterator for AnonChain<T> {
109    type Item = &'static T;
110
111    fn next(&mut self) -> Option<Self::Item> {
112        let mut curr = &mut self.iters[0];
113
114        if curr.curr == curr.len {
115            self.iters.remove(0);
116            if self.is_empty() {
117                return None;
118            }
119        }
120        curr = &mut self.iters[0];
121        Some(curr.next_unchecked())
122    }
123}
124
125#[derive(Clone)]
126pub struct AnonChainMut<T>
127where
128    T: Any + 'static,
129{
130    iters: Vec<AnonIterMut<T>>,
131}
132
133impl<T> AnonChainMut<T> 
134where
135    T: 'static,
136{
137    pub fn push(&mut self, iter: AnonIterMut<T>) {
138        self.iters.push(iter)
139    }
140
141    pub fn new() -> Self {
142        Self {
143            iters: Vec::with_capacity(16),
144        }
145    }
146
147    pub fn is_empty(&self) -> bool {
148        self.iters.is_empty()
149    }
150}
151
152impl<T> Iterator for AnonChainMut<T> {
153    type Item = &'static mut T;
154
155    fn next(&mut self) -> Option<Self::Item> {
156        let mut curr = &mut self.iters[0];
157
158        if curr.curr == curr.len {
159            self.iters.remove(0);
160            if self.is_empty() {
161                return None;
162            }
163        }
164        curr = &mut self.iters[0];
165        Some(curr.next_unchecked())
166    }
167}
168
169
170#[cfg(test)]
171mod tests {
172    #[test]
173    fn anon_iter() {
174
175    }
176
177    #[test]
178    fn anon_iter_mut() {
179
180    }
181
182    #[test]
183    fn anon_chain() {
184
185    }
186
187    #[test]
188    fn anon_chain_mut() {
189
190    }
191}