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
46
47
//! By-value and by-reference iterator objects for the various block variants.

use core::ops::Range;

macro_rules! impl_iterator_outer {
    ($name:ident $into_iter:ident $iter:ident) => {
        pub struct $into_iter<T> {
            pub(crate) block: $crate::$name<T>,
            pub(crate) index: Range<usize>,
        }

        impl<T> Iterator for $into_iter<T> {
            type Item = T;
            fn next(&mut self) -> Option<Self::Item> {
                Some(loop {
                    let idx = self.index.next()?;
                    if let Some(val) = self.block.remove(idx) {
                        break val;
                    }
                })
            }
        }

        pub struct $iter<'a, T> {
            pub(crate) block: &'a $crate::$name<T>,
            pub(crate) index: Range<usize>,
        }

        impl<'a, T> Iterator for $iter<'a, T> {
            type Item = &'a T;
            fn next(&mut self) -> Option<Self::Item> {
                Some(loop {
                    let idx = self.index.next()?;
                    if let Some(val) = self.block.get(idx) {
                        break val;
                    }
                })
            }
        }
    };
}

impl_iterator_outer!(Block8 Block8IntoIter Block8Iter);
impl_iterator_outer!(Block16 Block16IntoIter Block16Iter);
impl_iterator_outer!(Block32 Block32IntoIter Block32Iter);
impl_iterator_outer!(Block64 Block64IntoIter Block64Iter);
impl_iterator_outer!(Block128 Block128IntoIter Block128Iter);