|
1 |
| -use super::{Init, TypeFn}; |
| 1 | +use core::mem::MaybeUninit; |
| 2 | + |
| 3 | +use super::Init; |
2 | 4 |
|
3 | 5 | /// Untyped storage of the specified size and alignment.
|
4 | 6 | /// This is analogous to C++'s [`std::aligned_storage_t`].
|
5 | 7 | ///
|
6 | 8 | /// [`std::aligned_storage_t`]: https://en.cppreference.com/w/cpp/types/aligned_storage
|
7 |
| -/// |
8 |
| -/// This type alias expands to something like the following: |
9 |
| -/// |
10 |
| -/// ```rust,ignore |
11 |
| -/// #[repr(align(8))] |
12 |
| -/// #[derive(Clone, Copy)] |
13 |
| -/// struct AlignedStorage_256_8([u8; 256]); |
14 |
| -/// impl Init for AlignedStorage_256_8 { /* ... */ } |
15 |
| -/// ``` |
16 |
| -pub type AlignedStorage<const LEN: usize, const ALIGN: usize> = |
17 |
| - <AlignedStorageFn<LEN, ALIGN> as TypeFn>::Output; |
18 |
| - |
19 |
| -#[doc(hidden)] |
20 |
| -pub struct AlignedStorageFn<const LEN: usize, const ALIGN: usize>; |
21 |
| - |
22 |
| -#[doc(hidden)] |
23 |
| -pub mod aligned_storage_0b1 { |
24 |
| - /// Implements `TypeFn` on `AlignedStorageFn` for each possible alignemtn |
25 |
| - /// value. |
26 |
| - macro_rules! impl_aligned_storage_fn { |
27 |
| - ($align:tt, $($rest:tt)*) => { |
28 |
| - use super::{TypeFn, Init, AlignedStorageFn}; |
29 |
| - |
30 |
| - impl<const LEN: usize> TypeFn for AlignedStorageFn<LEN, $align> { |
31 |
| - type Output = Bytes<LEN>; |
32 |
| - } |
33 |
| - |
34 |
| - #[repr(align($align))] |
35 |
| - #[derive(Clone, Copy)] |
36 |
| - pub struct Bytes<const LEN: usize>(pub [u8; LEN]); |
37 |
| - |
38 |
| - impl<const LEN: usize> Init for Bytes<LEN> { |
39 |
| - const INIT: Self = Self([0; LEN]); |
40 |
| - } |
41 |
| - |
42 |
| - // It's not allowed to define multiple items with identical names |
43 |
| - // in the same scope. Macros such as `concat!` don't work in an |
44 |
| - // identifier position. |
45 |
| - // The solution? Define them in child modules! As a bonus, these |
46 |
| - // types receive paths remotely resembling the binary representation |
47 |
| - // of alignments, for example: |
48 |
| - // `aligned_storage_0b1::_0::_0::_0::_0::Bytes` (`0b10000`-byte |
49 |
| - // alignment). |
50 |
| - pub mod _0 { |
51 |
| - impl_aligned_storage_fn! { $($rest)* } |
52 |
| - } |
53 |
| - }; |
54 |
| - () => {}; |
55 |
| - } |
| 9 | +#[repr(C)] |
| 10 | +#[derive(Clone, Copy)] |
| 11 | +pub struct AlignedStorage<const LEN: usize, const ALIGN: usize>( |
| 12 | + elain::Align<ALIGN>, |
| 13 | + [MaybeUninit<u8>; LEN], |
| 14 | +) |
| 15 | +where |
| 16 | + elain::Align<ALIGN>: elain::Alignment; |
| 17 | + |
| 18 | +impl<const LEN: usize, const ALIGN: usize> Init for AlignedStorage<LEN, ALIGN> |
| 19 | +where |
| 20 | + elain::Align<ALIGN>: elain::Alignment, |
| 21 | +{ |
| 22 | + const INIT: Self = Self(elain::Align::NEW, [MaybeUninit::uninit(); LEN]); |
| 23 | +} |
56 | 24 |
|
57 |
| - impl_aligned_storage_fn! { |
58 |
| - 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, |
| 25 | +#[cfg(test)] |
| 26 | +mod tests { |
| 27 | + use super::*; |
| 28 | + #[test] |
| 29 | + fn size_align() { |
| 30 | + use core::alloc::Layout; |
| 31 | + |
| 32 | + macro test($len:expr, $align:expr) {{ |
| 33 | + let layout = Layout::new::<AlignedStorage<$len, $align>>(); |
| 34 | + dbg!(layout); |
| 35 | + assert_eq!(layout.align(), $align); |
| 36 | + assert_eq!(layout.size(), ($len + $align - 1) / $align * $align); |
| 37 | + }} |
| 38 | + |
| 39 | + macro test_outer($len:expr) { |
| 40 | + test!($len, 1); |
| 41 | + test!($len, 2); |
| 42 | + test!($len, 4); |
| 43 | + test!($len, 8); |
| 44 | + test!($len, 16); |
| 45 | + test!($len, 32); |
| 46 | + test!($len, 1024); |
| 47 | + } |
| 48 | + |
| 49 | + test_outer!(0); |
| 50 | + test_outer!(1); |
| 51 | + test_outer!(10); |
| 52 | + test_outer!(100); |
| 53 | + test_outer!(1000); |
| 54 | + test_outer!(1234); |
| 55 | + test_outer!(4321); |
| 56 | + test_outer!(10000); |
| 57 | + test_outer!(30000); |
59 | 58 | }
|
60 | 59 | }
|
0 commit comments