precomputed_map/
aligned.rs

1use core::mem;
2use core::marker::PhantomData;
3use crate::store::AsData;
4
5pub struct AlignedBytes<const B: usize, T> {
6    pub align: [T; 0],
7    pub bytes: [u8; B]
8}
9
10#[derive(Clone, Copy)]
11pub struct AlignedArray<const B: usize, T, DATA> {
12    pub bytes: DATA,
13    _phantom: PhantomData<[T; B]>
14}
15
16impl<'data, const B: usize, DATA> AlignedArray<B, u32, DATA>
17where
18    DATA: AsData<Data = &'data [u8; B]>
19{
20    pub const LEN: usize = {
21        if B % mem::size_of::<u32>() != 0 {
22            panic!();
23        }
24
25        B / mem::size_of::<u32>()
26    };
27
28    pub const fn new(bytes: DATA) -> Self {
29        AlignedArray { bytes, _phantom: PhantomData }
30    }
31    
32    #[inline]
33    pub fn get(&self, index: usize) -> Option<u32> {
34        let size = mem::size_of::<u32>();
35        let index = index * size;
36
37        if B >= index + size {
38            let buf = self.bytes.as_data()[index..][..size].try_into().unwrap();
39            Some(u32::from_le_bytes(buf))
40        } else {
41            None
42        }
43    }
44}