Function std::intrinsics::align_offset
[−]
[src]
pub unsafe extern "rust-intrinsic" fn align_offset(
ptr: *const (),
align: usize
) -> usize
🔬 This is a nightly-only experimental API. (core_intrinsics
)
intrinsics are unlikely to ever be stabilized, instead they should be used through stabilized interfaces in the rest of the standard library
Computes the byte offset that needs to be applied to ptr
in order to
make it aligned to align
.
If it is not possible to align ptr
, the implementation returns
usize::max_value()
.
There are no guarantees whatsover that offsetting the pointer will not
overflow or go beyond the allocation that ptr
points into.
It is up to the caller to ensure that the returned offset is correct
in all terms other than alignment.
Examples
Accessing adjacent u8
as u16
let x = [5u8, 6u8, 7u8, 8u8, 9u8]; let ptr = &x[n] as *const u8; let offset = align_offset(ptr as *const (), align_of::<u16>()); if offset < x.len() - n - 1 { let u16_ptr = ptr.offset(offset as isize) as *const u16; assert_ne!(*u16_ptr, 500); } else { // while the pointer can be aligned via `offset`, it would point // outside the allocation }Run