From 371f6023e7c415a1c2332686dfe799f1bc741f8f Mon Sep 17 00:00:00 2001 From: Mathis Bottinelli Date: Wed, 14 May 2025 01:07:31 +0200 Subject: [PATCH 1/3] implement `ptr::is_aligned_for` and `NonNull::is_aligned_for`. --- library/core/src/ptr/const_ptr.rs | 31 +++++++++++++++++++++++++++++++ library/core/src/ptr/mut_ptr.rs | 31 +++++++++++++++++++++++++++++++ library/core/src/ptr/non_null.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 35089b4853d7f..317a1687dbb86 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 + T: 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..8b22b99846a48 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 + T: 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..2ca2574a2f4a4 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -1285,6 +1285,37 @@ impl NonNull { self.as_ptr().is_aligned() } + /// Returns whether the pointer is properly aligned for `T`. + /// + /// # Examples + /// + /// ``` + /// 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 + T: 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, From 8d0a7bf76579a8e128f420b59562e50a7da7795f Mon Sep 17 00:00:00 2001 From: Mathis Bottinelli Date: Wed, 14 May 2025 08:46:36 +0200 Subject: [PATCH 2/3] ptr::is_aligned_for - remove T: Sized constraint --- library/core/src/ptr/const_ptr.rs | 8 ++++---- library/core/src/ptr/mut_ptr.rs | 8 ++++---- library/core/src/ptr/non_null.rs | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 317a1687dbb86..4b04e208d456a 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1444,11 +1444,11 @@ impl *const T { /// /// ``` /// #![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); @@ -1462,9 +1462,9 @@ impl *const T { #[must_use] #[inline] #[unstable(feature = "pointer_is_aligned_for", issue = "140980")] - pub fn is_aligned_for(self) -> bool + pub fn is_aligned_for(self) -> bool where - T: Sized, + U: Sized, { self.is_aligned_to(align_of::()) } diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 8b22b99846a48..aac58d34de32a 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -1699,11 +1699,11 @@ impl *mut T { /// /// ``` /// #![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); @@ -1717,9 +1717,9 @@ impl *mut T { #[must_use] #[inline] #[unstable(feature = "pointer_is_aligned_for", issue = "140980")] - pub fn is_aligned_for(self) -> bool + pub fn is_aligned_for(self) -> bool where - T: Sized, + U: Sized, { self.is_aligned_to(align_of::()) } diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index 2ca2574a2f4a4..fbeadcd8cad2f 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -1309,9 +1309,9 @@ impl NonNull { #[must_use] #[inline] #[unstable(feature = "pointer_is_aligned_for", issue = "140980")] - pub fn is_aligned_for(self) -> bool + pub fn is_aligned_for(self) -> bool where - T: Sized, + U: Sized, { self.as_ptr().is_aligned_for::() } From e51e90894d9482883509d25a8d1de85e59907ab8 Mon Sep 17 00:00:00 2001 From: Mathis Bottinelli Date: Wed, 14 May 2025 09:17:12 +0200 Subject: [PATCH 3/3] NonNull::is_aligned_for: fix doctest --- library/core/src/ptr/non_null.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index fbeadcd8cad2f..a4bf28621e37c 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -1290,6 +1290,7 @@ impl NonNull { /// # Examples /// /// ``` + /// #![feature(pointer_is_aligned_for)] /// use std::ptr::NonNull; /// /// // On some platforms, the alignment of i32 is less than 4.