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
56
57
58
59
use core::mem::MaybeUninit;
use super::Init;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct AlignedStorage<const LEN: usize, const ALIGN: usize>(
elain::Align<ALIGN>,
[MaybeUninit<u8>; LEN],
)
where
elain::Align<ALIGN>: elain::Alignment;
impl<const LEN: usize, const ALIGN: usize> Init for AlignedStorage<LEN, ALIGN>
where
elain::Align<ALIGN>: elain::Alignment,
{
const INIT: Self = Self(elain::Align::NEW, MaybeUninit::uninit_array());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_align() {
use core::alloc::Layout;
macro test($len:expr, $align:expr) {{
let layout = Layout::new::<AlignedStorage<$len, $align>>();
dbg!(layout);
assert_eq!(layout.align(), $align);
assert_eq!(layout.size(), ($len + $align - 1) / $align * $align);
}}
macro test_outer($len:expr) {
test!($len, 1);
test!($len, 2);
test!($len, 4);
test!($len, 8);
test!($len, 16);
test!($len, 32);
test!($len, 1024);
}
test_outer!(0);
test_outer!(1);
test_outer!(10);
test_outer!(100);
test_outer!(1000);
test_outer!(1234);
test_outer!(4321);
test_outer!(10000);
test_outer!(30000);
}
}