eta_algorithms/data_structs/array/
iterator.rs

1use std::marker::PhantomData;
2
3pub struct ArrayIterator<'a, T>
4where
5    T: Copy + Sized,
6{
7    #[allow(dead_code)]
8    pub(crate) phantom_data: &'a PhantomData<()>,
9    pub(crate) data: *mut T,
10    pub(crate) end: *mut T,
11}
12
13pub struct ArrayIteratorMut<'a, T>
14where
15    T: Copy + Sized,
16{
17    #[allow(dead_code)]
18    pub(crate) phantom_data: &'a mut PhantomData<()>,
19    pub(crate) data: *mut T,
20    pub(crate) end: *mut T,
21}
22
23macro_rules! impl_iterator {
24    ($name:ident; $item:ty; $mutability:tt) => {
25        impl<'a, T: 'a> Iterator for $name<'a, T>
26        where
27            T: Copy + Sized,
28        {
29            type Item = $item;
30            fn next(&mut self) -> Option<Self::Item> {
31                if self.data >= self.end {
32                    return None;
33                }
34                unsafe {
35                    let item = (self.data).$mutability().unwrap();
36                    self.data = self.data.offset(1);
37                    Some(item)
38                }
39            }
40        }
41    };
42}
43
44impl_iterator!(ArrayIterator; &'a T; as_ref);
45impl_iterator!(ArrayIteratorMut; &'a mut T; as_mut);