Skip to content

Implement ptr::is_aligned_for and NonNull::is_aligned_for. #140982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,37 @@ impl<T: ?Sized> *const T {
self.is_aligned_to(align_of::<T>())
}

/// Returns whether the pointer is properly aligned for `U`.
///
/// # Examples
///
/// ```
/// #![feature(pointer_is_aligned_for)]
///
/// // On some platforms, the alignment of i32 is less than 4.
/// #[repr(align(4))]
/// struct AlignedI32(i32);
///
/// // On some platforms, the alignment of u32 is less than 4.
/// #[repr(align(4))]
/// struct AlignedU32(u32);
///
/// let data = AlignedI32(42);
/// let ptr = &data as *const AlignedI32;
///
/// assert!(ptr.is_aligned_for::<AlignedU32>());
/// assert!(!ptr.wrapping_byte_add(1).is_aligned_for::<AlignedU32>());
/// ```
#[must_use]
#[inline]
#[unstable(feature = "pointer_is_aligned_for", issue = "140980")]
pub fn is_aligned_for<U>(self) -> bool
where
U: Sized,
{
self.is_aligned_to(align_of::<U>())
}

/// Returns whether the pointer is aligned to `align`.
///
/// For non-`Sized` pointees this operation considers only the data pointer,
Expand Down
31 changes: 31 additions & 0 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,37 @@ impl<T: ?Sized> *mut T {
self.is_aligned_to(align_of::<T>())
}

/// Returns whether the pointer is properly aligned for `U`.
///
/// # Examples
///
/// ```
/// #![feature(pointer_is_aligned_for)]
///
/// // On some platforms, the alignment of i32 is less than 4.
/// #[repr(align(4))]
/// struct AlignedI32(i32);
///
/// // On some platforms, the alignment of u32 is less than 4.
/// #[repr(align(4))]
/// struct AlignedU32(u32);
///
/// let mut data = AlignedI32(42);
/// let ptr = &mut data as *mut AlignedI32;
///
/// assert!(ptr.is_aligned_for::<AlignedU32>());
/// assert!(!ptr.wrapping_byte_add(1).is_aligned_for::<AlignedU32>());
/// ```
#[must_use]
#[inline]
#[unstable(feature = "pointer_is_aligned_for", issue = "140980")]
pub fn is_aligned_for<U>(self) -> bool
where
U: Sized,
{
self.is_aligned_to(align_of::<U>())
}

/// Returns whether the pointer is aligned to `align`.
///
/// For non-`Sized` pointees this operation considers only the data pointer,
Expand Down
32 changes: 32 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,38 @@ impl<T: ?Sized> NonNull<T> {
self.as_ptr().is_aligned()
}

/// Returns whether the pointer is properly aligned for `T`.
///
/// # Examples
///
/// ```
/// #![feature(pointer_is_aligned_for)]
/// use std::ptr::NonNull;
///
/// // On some platforms, the alignment of i32 is less than 4.
/// #[repr(align(4))]
/// struct AlignedI32(i32);
///
/// // On some platforms, the alignment of u32 is less than 4.
/// #[repr(align(4))]
/// struct AlignedU32(u32);
///
/// let data = AlignedI32(42);
/// let ptr = NonNull::<AlignedI32>::from(&data);
///
/// assert!(ptr.is_aligned_for::<AlignedU32>());
/// assert!(!NonNull::new(ptr.as_ptr().wrapping_byte_add(1)).unwrap().is_aligned_for::<AlignedU32>());
/// ```
#[must_use]
#[inline]
#[unstable(feature = "pointer_is_aligned_for", issue = "140980")]
pub fn is_aligned_for<U>(self) -> bool
where
U: Sized,
{
self.as_ptr().is_aligned_for::<U>()
}

/// Returns whether the pointer is aligned to `align`.
///
/// For non-`Sized` pointees this operation considers only the data pointer,
Expand Down
Loading