Function core::ptr::align_offset

source ·
pub(crate) const unsafe fn align_offset<T: Sized>(
    p: *const T,
    a: usize
) -> usize
Expand description

Align pointer p.

Calculate offset (in terms of elements of size_of::<T>() stride) that has to be applied to pointer p so that pointer p would get aligned to a.

Safety

a must be a power of two.

Notes

This implementation has been carefully tailored to not panic. It is UB for this to panic. The only real change that can be made here is change of INV_TABLE_MOD_16 and associated constants.

If we ever decide to make it possible to call the intrinsic with a that is not a power-of-two, it will probably be more prudent to just change to a naive implementation rather than trying to adapt this to accommodate that change.

Any questions go to @nagisa.