Skip to content

Commit e748f09

Browse files
committed
ptr::try_cast_aligned: fix doc
1 parent 5ab5259 commit e748f09

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,23 @@ impl<T: ?Sized> *const T {
6868

6969
/// Try to cast to a pointer of another type by checking aligment.
7070
///
71-
/// If the pointer is well aligned to the target type, it will be casted
72-
/// to the target type. Otherwise, `None` is returned.
71+
/// If the pointer is properly aligned to the target type, it will be
72+
/// cast to the target type. Otherwise, `None` is returned.
7373
///
7474
/// # Examples
7575
///
7676
/// ```rust
7777
/// #![feature(pointer_try_cast_aligned)]
7878
///
79-
/// // On some platforms, the alignment of i32 is less than 4.
80-
/// #[repr(align(4))]
81-
/// struct AlignedI32(i32);
79+
/// let aligned: *const u8 = 0x1000 as _;
80+
///
81+
/// // i32 has at most 4-byte alignment, so this will succeed
82+
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
83+
///
84+
/// let unaligned: *const u8 = 0x1001 as _;
8285
///
83-
/// // This pointer is aligned to 4 bytes
84-
/// let ptr: *const u8 = 0x1000 as _;
85-
/// assert!(ptr.try_cast_aligned::<AlignedI32>().is_some());
86+
/// // i32 has at least 2-byte alignment, so this will fail
87+
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
8688
/// ```
8789
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
8890
#[must_use = "this returns the result of the operation, \

library/core/src/ptr/mut_ptr.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,23 @@ impl<T: ?Sized> *mut T {
5050

5151
/// Try to cast to a pointer of another type by checking aligment.
5252
///
53-
/// If the pointer is well aligned to the target type, it will be casted
54-
/// to the target type. Otherwise, `None` is returned.
53+
/// If the pointer is properly aligned to the target type, it will be
54+
/// cast to the target type. Otherwise, `None` is returned.
5555
///
5656
/// # Examples
5757
///
5858
/// ```rust
5959
/// #![feature(pointer_try_cast_aligned)]
6060
///
61-
/// // On some platforms, the alignment of i32 is less than 4.
62-
/// #[repr(align(4))]
63-
/// struct AlignedI32(i32);
61+
/// let aligned: *mut u8 = 0x1000 as _;
62+
///
63+
/// // i32 has at most 4-byte alignment, so this will succeed
64+
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
65+
///
66+
/// let unaligned: *mut u8 = 0x1001 as _;
6467
///
65-
/// // This pointer is aligned to 4 bytes
66-
/// let ptr: *mut u8 = 0x1000 as _;
67-
/// assert!(ptr.try_cast_aligned::<AlignedI32>().is_some());
68+
/// // i32 has at least 2-byte alignment, so this will fail
69+
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
6870
/// ```
6971
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
7072
#[must_use = "this returns the result of the operation, \

library/core/src/ptr/non_null.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,23 +490,24 @@ impl<T: ?Sized> NonNull<T> {
490490

491491
/// Try to cast to a pointer of another type by checking aligment.
492492
///
493-
/// If the pointer is well aligned to the target type, it will be casted
494-
/// to the target type. Otherwise, `None` is returned.
493+
/// If the pointer is properly aligned to the target type, it will be
494+
/// cast to the target type. Otherwise, `None` is returned.
495495
///
496496
/// # Examples
497497
///
498498
/// ```rust
499499
/// #![feature(pointer_try_cast_aligned)]
500500
/// use std::ptr::NonNull;
501501
///
502-
/// // On some platforms, the alignment of i32 is less than 4.
503-
/// #[repr(align(4))]
504-
/// struct AlignedI32(i32);
502+
/// let aligned: NonNull<u8> = NonNull::new(0x1000 as _).unwrap();
503+
///
504+
/// // i32 has at most 4-byte alignment, so this will succeed
505+
/// assert!(aligned.try_cast_aligned::<i32>().is_some());
506+
///
507+
/// let unaligned: NonNull<u8> = NonNull::new(0x1001 as _).unwrap();
505508
///
506-
/// // This pointer is aligned to 4 bytes
507-
/// let ptr: *mut u8 = 0x1000 as _;
508-
/// let non_null = NonNull::new(ptr).unwrap();
509-
/// assert!(non_null.try_cast_aligned::<AlignedI32>().is_some());
509+
/// // i32 has at least 2-byte alignment, so this will fail
510+
/// assert!(unaligned.try_cast_aligned::<i32>().is_none());
510511
/// ```
511512
#[unstable(feature = "pointer_try_cast_aligned", issue = "141221")]
512513
#[must_use = "this returns the result of the operation, \

0 commit comments

Comments
 (0)