nvme/ds/
namespace.rs

1#[derive(Debug, Default, Clone, Copy)]
2#[repr(transparent)]
3pub struct NamespaceId(u32);
4
5impl NamespaceId {
6    pub fn new(id: impl Into<u32>) -> Self {
7        Self(id.into())
8    }
9}
10
11pub struct NamespaceList<'a, const BYTES: usize> {
12    data: &'a [u8; BYTES],
13}
14
15impl<'a, const BYTES: usize> NamespaceList<'a, BYTES> {
16    pub fn new(data: &'a [u8; BYTES]) -> Self {
17        Self { data }
18    }
19
20    pub fn nr_bytes(&self) -> usize {
21        BYTES
22    }
23}
24
25pub struct NamespaceListIter<'a, const BYTES: usize> {
26    list: NamespaceList<'a, BYTES>,
27    pos: usize,
28}
29
30impl<'a, const BYTES: usize> Iterator for NamespaceListIter<'a, BYTES> {
31    type Item = NamespaceId;
32
33    fn next(&mut self) -> Option<Self::Item> {
34        if self.pos >= BYTES {
35            return None;
36        }
37        let by = self.list.data[self.pos..(self.pos + 4)].as_ptr() as *const u32;
38        let val = unsafe { *by };
39        if val == 0 {
40            return None;
41        }
42        self.pos += 4;
43        Some(NamespaceId(val))
44    }
45}
46
47impl<'a, const BYTES: usize> IntoIterator for NamespaceList<'a, BYTES> {
48    type Item = NamespaceId;
49
50    type IntoIter = NamespaceListIter<'a, BYTES>;
51
52    fn into_iter(self) -> Self::IntoIter {
53        NamespaceListIter { list: self, pos: 0 }
54    }
55}