eta_algorithms/data_structs/array/
iterator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::marker::PhantomData;

pub struct ArrayIterator<'a, T>
where
    T: Copy + Sized,
{
    #[allow(dead_code)]
    pub(crate) phantom_data: &'a PhantomData<()>,
    pub(crate) data: *mut T,
    pub(crate) end: *mut T,
}

pub struct ArrayIteratorMut<'a, T>
where
    T: Copy + Sized,
{
    #[allow(dead_code)]
    pub(crate) phantom_data: &'a mut PhantomData<()>,
    pub(crate) data: *mut T,
    pub(crate) end: *mut T,
}

macro_rules! impl_iterator {
    ($name:ident; $item:ty; $mutability:tt) => {
        impl<'a, T: 'a> Iterator for $name<'a, T>
        where
            T: Copy + Sized,
        {
            type Item = $item;
            fn next(&mut self) -> Option<Self::Item> {
                if self.data >= self.end {
                    return None;
                }
                unsafe {
                    let item = (self.data).$mutability().unwrap();
                    self.data = self.data.offset(1);
                    Some(item)
                }
            }
        }
    };
}

impl_iterator!(ArrayIterator; &'a T; as_ref);
impl_iterator!(ArrayIteratorMut; &'a mut T; as_mut);