Trait uefi::data_types::Align
source · [−]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
Provided methods
fn offset_up_to_alignment(val: usize) -> usize
fn offset_up_to_alignment(val: usize) -> usize
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
.
fn round_up_to_alignment(val: usize) -> usize
fn round_up_to_alignment(val: usize) -> usize
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.
fn assert_aligned(storage: &mut [u8])
fn assert_aligned(storage: &mut [u8])
Assert that some storage is correctly aligned for this type