Skip to content

Commit 204d174

Browse files
committed
try_cast_aligned: avoid bare int-to-ptr casts
1 parent 6eef33b commit 204d174

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,13 @@ impl<T: ?Sized> *const T {
7676
/// ```rust
7777
/// #![feature(pointer_try_cast_aligned)]
7878
///
79-
/// let aligned: *const u8 = 0x1000 as _;
79+
/// let x = 0u64;
8080
///
81-
/// // i32 has at most 4-byte alignment, so this will succeed
82-
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
81+
/// let aligned: *const u64 = &x;
82+
/// let unaligned = unsafe { aligned.byte_add(1) };
8383
///
84-
/// let unaligned: *const u8 = 0x1001 as _;
85-
///
86-
/// // i32 has at least 2-byte alignment, so this will fail
87-
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
84+
/// assert!(aligned.try_cast_aligned::<u32>().is_some());
85+
/// assert!(unaligned.try_cast_aligned::<u32>().is_none());
8886
/// ```
8987
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
9088
#[must_use = "this returns the result of the operation, \

library/core/src/ptr/mut_ptr.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ impl<T: ?Sized> *mut T {
5858
/// ```rust
5959
/// #![feature(pointer_try_cast_aligned)]
6060
///
61-
/// let aligned: *mut u8 = 0x1000 as _;
61+
/// let mut x = 0u64;
6262
///
63-
/// // i32 has at most 4-byte alignment, so this will succeed
64-
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
63+
/// let aligned: *mut u64 = &mut x;
64+
/// let unaligned = unsafe { aligned.byte_add(1) };
6565
///
66-
/// let unaligned: *mut u8 = 0x1001 as _;
67-
///
68-
/// // i32 has at least 2-byte alignment, so this will fail
69-
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
66+
/// assert!(aligned.try_cast_aligned::<u32>().is_some());
67+
/// assert!(unaligned.try_cast_aligned::<u32>().is_none());
7068
/// ```
7169
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
7270
#[must_use = "this returns the result of the operation, \

library/core/src/ptr/non_null.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,15 +501,13 @@ impl<T: ?Sized> NonNull<T> {
501501
/// #![feature(pointer_try_cast_aligned)]
502502
/// use std::ptr::NonNull;
503503
///
504-
/// let aligned: NonNull<u8> = NonNull::new(0x1000 as _).unwrap();
504+
/// let mut x = 0u64;
505505
///
506-
/// // i32 has at most 4-byte alignment, so this will succeed
507-
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
506+
/// let aligned = NonNull::from_ref(&mut x);
507+
/// let unaligned = unsafe { aligned.byte_add(1) };
508508
///
509-
/// let unaligned: NonNull<u8> = NonNull::new(0x1001 as _).unwrap();
510-
///
511-
/// // i32 has at least 2-byte alignment, so this will fail
512-
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
509+
/// assert!(aligned.try_cast_aligned::<u32>().is_some());
510+
/// assert!(unaligned.try_cast_aligned::<u32>().is_none());
513511
/// ```
514512
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
515513
#[must_use = "this returns the result of the operation, \

0 commit comments

Comments
 (0)