37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
|
|
pub struct Align<const ALIGNMENT: usize>( <Self as Aligned>::Alignment ) where Self: Aligned;
|
|
pub trait Aligned { type Alignment; }
|
|
|
|
impl <const ALIGNMENT: usize> Align<ALIGNMENT> where Self: Aligned {
|
|
pub const fn new() -> Self { unsafe { core::mem::zeroed() } }
|
|
}
|
|
|
|
unsafe impl <const ALIGNMENT: usize> crate::data::Zeroed for Align<ALIGNMENT> where Self: Aligned {}
|
|
|
|
impl <const ALIGNMENT: usize> Default for Align<ALIGNMENT> where Self: Aligned {
|
|
fn default() -> Self { Self::new() }
|
|
}
|
|
|
|
macro_rules! gen_alignment {
|
|
($($alignment:literal),+) => {
|
|
$(
|
|
const _: () = {
|
|
#[repr(align($alignment))] pub struct Anon;
|
|
impl Aligned for Align<$alignment> {
|
|
type Alignment = Anon;
|
|
}
|
|
assert!(align_of::<Align<$alignment>>() == $alignment)
|
|
};
|
|
)+
|
|
};
|
|
}
|
|
|
|
gen_alignment!(
|
|
1, 2, 4, 8, 16, 32, 64, 128, 256, 512,
|
|
1024, 2048, 4096, 8192, 16384, 32768,
|
|
65536, 131072, 262144, 524288, 1048576,
|
|
2097152, 4194304, 8388608, 16777216,
|
|
33554432, 67108864, 134217728, 268435456,
|
|
536870912);
|
|
|