Struct dungeon_cell::Aligned
source · #[repr(C)]pub struct Aligned<T, A: ValidAlignment> {
pub value: T,
/* private fields */
}
Expand description
Force minimum alignment of a type.
Invariants
The wrapped value
is guaranteed, for specifically unsafe code, to have
a minimum alignment of A::VALUE
bytes. The wrapped value
is also guaranteed
to have a zero offset from the start of the alignment block. This makes
it sound to construct a reference the wrapped value
for a different type
with an alignment of maximum A::VALUE
bytes, given the other unsafe rules
are followed.
Additionally the layout of this struct is #[repr(C)]
with a single field
value
of type T
and a private zero sized type with an alignment of A::VALUE
bytes.
Examples
use std::mem::*;
use dungeon_cell::{Aligned, Alignment};
// 1 byte aligned buffer
let x: [u8; 4] = 1234567890u32.to_ne_bytes();
assert_eq!(size_of_val(&x), 4);
assert_eq!(align_of_val(&x), 1);
// 4 byte aligned buffer
let y = Aligned::<_, Alignment<4>>::new(x);
assert_eq!(size_of_val(&y), 4);
assert_eq!(align_of_val(&y), 4);
// this cast back to a u32 is sound because Aligned
// makes the value field aligned to 4 bytes.
let z = &y.value as *const [u8; 4] as *const u32;
let z = unsafe { &*z };
assert_eq!(z, &1234567890);
Fields§
§value: T
Stored value.
This field is guaranteed to be aligned to A
bytes.