Skip to content

Implement ptr::try_cast_aligned and NonNull::try_cast_aligned. #141222

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

Merged
merged 1 commit into from
May 21, 2025
Merged
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
28 changes: 28 additions & 0 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ impl<T: ?Sized> *const T {
self as _
}

/// Try to cast to a pointer of another type by checking aligment.
///
/// If the pointer is properly aligned to the target type, it will be
/// cast to the target type. Otherwise, `None` is returned.
///
/// # Examples
///
/// ```rust
/// #![feature(pointer_try_cast_aligned)]
///
/// let aligned: *const u8 = 0x1000 as _;
///
/// // i32 has at most 4-byte alignment, so this will succeed
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
///
/// let unaligned: *const u8 = 0x1001 as _;
///
/// // i32 has at least 2-byte alignment, so this will fail
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
/// ```
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub fn try_cast_aligned<U>(self) -> Option<*const U> {
if self.is_aligned_to(align_of::<U>()) { Some(self.cast()) } else { None }
}

/// Uses the address value in a new pointer of another type.
///
/// This operation will ignore the address part of its `meta` operand and discard existing
Expand Down
28 changes: 28 additions & 0 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,34 @@ impl<T: ?Sized> *mut T {
self as _
}

/// Try to cast to a pointer of another type by checking aligment.
///
/// If the pointer is properly aligned to the target type, it will be
/// cast to the target type. Otherwise, `None` is returned.
///
/// # Examples
///
/// ```rust
/// #![feature(pointer_try_cast_aligned)]
///
/// let aligned: *mut u8 = 0x1000 as _;
///
/// // i32 has at most 4-byte alignment, so this will succeed
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
///
/// let unaligned: *mut u8 = 0x1001 as _;
///
/// // i32 has at least 2-byte alignment, so this will fail
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
/// ```
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub fn try_cast_aligned<U>(self) -> Option<*mut U> {
if self.is_aligned_to(align_of::<U>()) { Some(self.cast()) } else { None }
}

/// Uses the address value in a new pointer of another type.
///
/// This operation will ignore the address part of its `meta` operand and discard existing
Expand Down
29 changes: 29 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,35 @@ impl<T: ?Sized> NonNull<T> {
unsafe { NonNull { pointer: self.as_ptr() as *mut U } }
}

/// Try to cast to a pointer of another type by checking aligment.
///
/// If the pointer is properly aligned to the target type, it will be
/// cast to the target type. Otherwise, `None` is returned.
///
/// # Examples
///
/// ```rust
/// #![feature(pointer_try_cast_aligned)]
/// use std::ptr::NonNull;
///
/// let aligned: NonNull<u8> = NonNull::new(0x1000 as _).unwrap();
///
/// // i32 has at most 4-byte alignment, so this will succeed
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
///
/// let unaligned: NonNull<u8> = NonNull::new(0x1001 as _).unwrap();
///
/// // i32 has at least 2-byte alignment, so this will fail
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
/// ```
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline]
pub fn try_cast_aligned<U>(self) -> Option<NonNull<U>> {
if self.is_aligned_to(align_of::<U>()) { Some(self.cast()) } else { None }
}

/// Adds an offset to a pointer.
///
/// `count` is in units of T; e.g., a `count` of 3 represents a pointer
Expand Down
Loading