pub trait Align {
    fn alignment() -> usize;

    fn offset_up_to_alignment(val: usize) -> usize { ... }
fn round_up_to_alignment(val: usize) -> usize { ... }
fn align_buf(buf: &mut [u8]) -> Option<&mut [u8]> { ... }
fn assert_aligned(storage: &mut [u8]) { ... } }
Expand description

Trait for querying the alignment of a struct.

For a statically-sized type the alignment can be retrieved with core::mem::align_of. For a dynamically-sized type (DST), core::mem::align_of_val provides the alignment given a reference. But in some cases it’s helpful to know the alignment of a DST prior to having a value, meaning there’s no reference to pass to align_of_val. For example, when using an API that creates a value using a [u8] buffer, the alignment of the buffer must be checked. The Align trait makes that possible by allowing the appropriate alignment to be manually specified.

Required methods

Required memory alignment for this type

Provided methods

Calculate the offset from val necessary to make it aligned, rounding up. For example, if val is 1 and the alignment is 8, this will return 7. Returns 0 if val == 0.

Round val up so that it is aligned.

Get a subslice of buf where the address of the first element is aligned. Returns None if no element of the buffer is aligned.

Assert that some storage is correctly aligned for this type

Implementors