precomputed_map/
aligned.rs

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