about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2025-05-22 08:24:40 +0200
committerRalf Jung <post@ralfj.de>2025-05-22 13:32:36 +0200
commit09ae053f7a6d29909a309953182b6f067717cc83 (patch)
tree4743c287bbfd05292ae764f25572d6715ca4516f
parent6eef33bb399cabfab16aa4e0825895f5f32f4e26 (diff)
downloadrust-09ae053f7a6d29909a309953182b6f067717cc83.tar.gz
rust-09ae053f7a6d29909a309953182b6f067717cc83.zip
try_cast_aligned: avoid bare int-to-ptr casts
-rw-r--r--library/core/src/ptr/const_ptr.rs12
-rw-r--r--library/core/src/ptr/mut_ptr.rs12
-rw-r--r--library/core/src/ptr/non_null.rs12
3 files changed, 15 insertions, 21 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs
index f6109cafe86..19ed7599a51 100644
--- a/library/core/src/ptr/const_ptr.rs
+++ b/library/core/src/ptr/const_ptr.rs
@@ -76,15 +76,13 @@ impl<T: ?Sized> *const T {
     /// ```rust
     /// #![feature(pointer_try_cast_aligned)]
     ///
-    /// let aligned: *const u8 = 0x1000 as _;
+    /// let x = 0u64;
     ///
-    /// // i32 has at most 4-byte alignment, so this will succeed
-    /// assert!(aligned.try_cast_aligned::<i32>().is_some());
+    /// let aligned: *const u64 = &x;
+    /// let unaligned = unsafe { aligned.byte_add(1) };
     ///
-    /// 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());
+    /// assert!(aligned.try_cast_aligned::<u32>().is_some());
+    /// assert!(unaligned.try_cast_aligned::<u32>().is_none());
     /// ```
     #[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
     #[must_use = "this returns the result of the operation, \
diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs
index 2662a4fdc31..53aa3ab4938 100644
--- a/library/core/src/ptr/mut_ptr.rs
+++ b/library/core/src/ptr/mut_ptr.rs
@@ -58,15 +58,13 @@ impl<T: ?Sized> *mut T {
     /// ```rust
     /// #![feature(pointer_try_cast_aligned)]
     ///
-    /// let aligned: *mut u8 = 0x1000 as _;
+    /// let mut x = 0u64;
     ///
-    /// // i32 has at most 4-byte alignment, so this will succeed
-    /// assert!(aligned.try_cast_aligned::<i32>().is_some());
+    /// let aligned: *mut u64 = &mut x;
+    /// let unaligned = unsafe { aligned.byte_add(1) };
     ///
-    /// 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());
+    /// assert!(aligned.try_cast_aligned::<u32>().is_some());
+    /// assert!(unaligned.try_cast_aligned::<u32>().is_none());
     /// ```
     #[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
     #[must_use = "this returns the result of the operation, \
diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs
index bb344c6a0d3..7c9b898f8e9 100644
--- a/library/core/src/ptr/non_null.rs
+++ b/library/core/src/ptr/non_null.rs
@@ -501,15 +501,13 @@ impl<T: ?Sized> NonNull<T> {
     /// #![feature(pointer_try_cast_aligned)]
     /// use std::ptr::NonNull;
     ///
-    /// let aligned: NonNull<u8> = NonNull::new(0x1000 as _).unwrap();
+    /// let mut x = 0u64;
     ///
-    /// // i32 has at most 4-byte alignment, so this will succeed
-    /// assert!(aligned.try_cast_aligned::<i32>().is_some());
+    /// let aligned = NonNull::from_mut(&mut x);
+    /// let unaligned = unsafe { aligned.byte_add(1) };
     ///
-    /// 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());
+    /// assert!(aligned.try_cast_aligned::<u32>().is_some());
+    /// assert!(unaligned.try_cast_aligned::<u32>().is_none());
     /// ```
     #[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
     #[must_use = "this returns the result of the operation, \