diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 35089b4853d7f..4b04e208d456a 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1438,6 +1438,37 @@ impl *const T { self.is_aligned_to(align_of::()) } + /// 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::()); + /// assert!(!ptr.wrapping_byte_add(1).is_aligned_for::()); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "pointer_is_aligned_for", issue = "140980")] + pub fn is_aligned_for(self) -> bool + where + U: Sized, + { + self.is_aligned_to(align_of::()) + } + /// Returns whether the pointer is aligned to `align`. /// /// For non-`Sized` pointees this operation considers only the data pointer, diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 9cf251742d427..aac58d34de32a 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -1693,6 +1693,37 @@ impl *mut T { self.is_aligned_to(align_of::()) } + /// 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::()); + /// assert!(!ptr.wrapping_byte_add(1).is_aligned_for::()); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "pointer_is_aligned_for", issue = "140980")] + pub fn is_aligned_for(self) -> bool + where + U: Sized, + { + self.is_aligned_to(align_of::()) + } + /// Returns whether the pointer is aligned to `align`. /// /// For non-`Sized` pointees this operation considers only the data pointer, diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 8b31328de047f..a4bf28621e37c 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -1285,6 +1285,38 @@ impl NonNull { 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::::from(&data); + /// + /// assert!(ptr.is_aligned_for::()); + /// assert!(!NonNull::new(ptr.as_ptr().wrapping_byte_add(1)).unwrap().is_aligned_for::()); + /// ``` + #[must_use] + #[inline] + #[unstable(feature = "pointer_is_aligned_for", issue = "140980")] + pub fn is_aligned_for(self) -> bool + where + U: Sized, + { + self.as_ptr().is_aligned_for::() + } + /// Returns whether the pointer is aligned to `align`. /// /// For non-`Sized` pointees this operation considers only the data pointer,