Skip to content

Commit 2c0186f

Browse files
committed
refactor(utils): replace the innards of AlignedStorage with ::elain
<https://crates.io/crates/elain>
1 parent 0414b03 commit 2c0186f

File tree

3 files changed

+59
-52
lines changed

3 files changed

+59
-52
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/r3/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tokenlock = { version = "0.3.4", default-features = false }
3333
arrayvec = { version = "0.7.1", default-features = false }
3434
bitflags = { version = "1.2.1" }
3535
chrono = { version = "0.4.13", optional = true, default-features = false }
36+
elain = { version = "0.3.0" }
3637

3738
[dev-dependencies]
3839
quickcheck_macros = "1.0.0"

src/r3/src/utils/aligned_storage.rs

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
1-
use super::{Init, TypeFn};
1+
use core::mem::MaybeUninit;
2+
3+
use super::Init;
24

35
/// Untyped storage of the specified size and alignment.
46
/// This is analogous to C++'s [`std::aligned_storage_t`].
57
///
68
/// [`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+
}
5624

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);
5958
}
6059
}

0 commit comments

Comments
 (0)