diff options
| author | Jakub Beránek <berykubik@gmail.com> | 2025-08-13 07:03:52 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-13 07:03:52 +0200 |
| commit | 8cb78fe3b63ab1f83d084411cb1f768ce9443a3d (patch) | |
| tree | b4277e319a2c50fa0ebec963b40249ee3800c804 /library/core/src/ptr | |
| parent | c4e82abe93f19878ba0ee09779ef18a53896150e (diff) | |
| parent | d6945f6d8c15bd692000cd7f8d76d171ae27b8c9 (diff) | |
Rollup merge of #145325 - clarfonthey:cast-init, r=scottmcm
Add `cast_init` and `cast_uninit` methods for pointers ACP: rust-lang/libs-team#627 Tracking issue: rust-lang/rust#145036 This includes an incredibly low-effort search to find uses that could be switched to using these methods. I only searched for `cast::<\w>` and `cast::<MaybeUninit` because there would otherwise be way too much to look through, and I also didn't modify anything inside submodules/subtrees.
Diffstat (limited to 'library/core/src/ptr')
| -rw-r--r-- | library/core/src/ptr/const_ptr.rs | 22 | ||||
| -rw-r--r-- | library/core/src/ptr/mut_ptr.rs | 25 | ||||
| -rw-r--r-- | library/core/src/ptr/non_null.rs | 22 |
3 files changed, 69 insertions, 0 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 8b3703bd4b3..6546dde39ac 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1430,6 +1430,28 @@ impl<T: PointeeSized> *const T { } } +impl<T> *const T { + /// Casts from a type to its maybe-uninitialized version. + #[must_use] + #[inline(always)] + #[unstable(feature = "cast_maybe_uninit", issue = "145036")] + pub const fn cast_uninit(self) -> *const MaybeUninit<T> { + self as _ + } +} +impl<T> *const MaybeUninit<T> { + /// Casts from a maybe-uninitialized type to its initialized version. + /// + /// This is always safe, since UB can only occur if the pointer is read + /// before being initialized. + #[must_use] + #[inline(always)] + #[unstable(feature = "cast_maybe_uninit", issue = "145036")] + pub const fn cast_init(self) -> *const T { + self as _ + } +} + impl<T> *const [T] { /// Returns the length of a raw slice. /// diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index af39ec86d7a..4add964141a 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -1687,6 +1687,31 @@ impl<T: PointeeSized> *mut T { } } +impl<T> *mut T { + /// Casts from a type to its maybe-uninitialized version. + /// + /// This is always safe, since UB can only occur if the pointer is read + /// before being initialized. + #[must_use] + #[inline(always)] + #[unstable(feature = "cast_maybe_uninit", issue = "145036")] + pub const fn cast_uninit(self) -> *mut MaybeUninit<T> { + self as _ + } +} +impl<T> *mut MaybeUninit<T> { + /// Casts from a maybe-uninitialized type to its initialized version. + /// + /// This is always safe, since UB can only occur if the pointer is read + /// before being initialized. + #[must_use] + #[inline(always)] + #[unstable(feature = "cast_maybe_uninit", issue = "145036")] + pub const fn cast_init(self) -> *mut T { + self as _ + } +} + impl<T> *mut [T] { /// Returns the length of a raw slice. /// diff --git a/library/core/src/ptr/non_null.rs b/library/core/src/ptr/non_null.rs index e72aa1da2cb..da382b8715e 100644 --- a/library/core/src/ptr/non_null.rs +++ b/library/core/src/ptr/non_null.rs @@ -1357,6 +1357,28 @@ impl<T: PointeeSized> NonNull<T> { } } +impl<T> NonNull<T> { + /// Casts from a type to its maybe-uninitialized version. + #[must_use] + #[inline(always)] + #[unstable(feature = "cast_maybe_uninit", issue = "145036")] + pub const fn cast_uninit(self) -> NonNull<MaybeUninit<T>> { + self.cast() + } +} +impl<T> NonNull<MaybeUninit<T>> { + /// Casts from a maybe-uninitialized type to its initialized version. + /// + /// This is always safe, since UB can only occur if the pointer is read + /// before being initialized. + #[must_use] + #[inline(always)] + #[unstable(feature = "cast_maybe_uninit", issue = "145036")] + pub const fn cast_init(self) -> NonNull<T> { + self.cast() + } +} + impl<T> NonNull<[T]> { /// Creates a non-null raw slice from a thin pointer and a length. /// |
