Add convenience byte offset/check align functions to pointers by WaffleLapkin · Pull Request #95643 · rust-lang/rust (original) (raw)

This PR adds the following APIs:

impl *const T { // feature gates pointer_byte_offsets and `const_pointer_byte_offsets pub const unsafe fn byte_offset(self, count: isize) -> Self; pub const fn wrapping_byte_offset(self, count: isize) -> Self; pub const unsafe fn byte_offset_from(self, origin: *const T) -> isize; pub const unsafe fn byte_add(self, count: usize) -> Self; pub const unsafe fn byte_sub(self, count: usize) -> Self; pub const fn wrapping_byte_add(self, count: usize) -> Self; pub const fn wrapping_byte_sub(self, count: usize) -> Self;

// feature gate `pointer_is_aligned`
pub fn is_aligned(self) -> bool where T: Sized;
pub fn is_aligned_to(self, align: usize) -> bool;

} // ... and the same for *mut T

Note that all functions except is_aligned do not require T: Sized as their pointee-sized-offset counterparts.

cc @oli-obk (you may want to check that I've correctly placed consts)
cc @RalfJung