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
48
49
50
51
52
53
54
55
#[derive(Debug, Default, Clone, Copy)]
#[repr(transparent)]
pub struct NamespaceId(u32);

impl NamespaceId {
    pub fn new(id: impl Into<u32>) -> Self {
        Self(id.into())
    }
}

pub struct NamespaceList<'a, const BYTES: usize> {
    data: &'a [u8; BYTES],
}

impl<'a, const BYTES: usize> NamespaceList<'a, BYTES> {
    pub fn new(data: &'a [u8; BYTES]) -> Self {
        Self { data }
    }

    pub fn nr_bytes(&self) -> usize {
        BYTES
    }
}

pub struct NamespaceListIter<'a, const BYTES: usize> {
    list: NamespaceList<'a, BYTES>,
    pos: usize,
}

impl<'a, const BYTES: usize> Iterator for NamespaceListIter<'a, BYTES> {
    type Item = NamespaceId;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= BYTES {
            return None;
        }
        let by = self.list.data[self.pos..(self.pos + 4)].as_ptr() as *const u32;
        let val = unsafe { *by };
        if val == 0 {
            return None;
        }
        self.pos += 4;
        Some(NamespaceId(val))
    }
}

impl<'a, const BYTES: usize> IntoIterator for NamespaceList<'a, BYTES> {
    type Item = NamespaceId;

    type IntoIter = NamespaceListIter<'a, BYTES>;

    fn into_iter(self) -> Self::IntoIter {
        NamespaceListIter { list: self, pos: 0 }
    }
}