Skip to content

Add cast_init and cast_uninit methods for pointers #145325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
#![feature(async_iterator)]
#![feature(bstr)]
#![feature(bstr_internals)]
#![feature(cast_maybe_uninit)]
#![feature(char_internals)]
#![feature(char_max_len)]
#![feature(clone_to_uninit)]
Expand Down
2 changes: 1 addition & 1 deletion library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3176,7 +3176,7 @@ impl<T, A: Allocator> Vec<T, A> {
// - but the allocation extends out to `self.buf.capacity()` elements, possibly
// uninitialized
let spare_ptr = unsafe { ptr.add(self.len) };
let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
let spare_ptr = spare_ptr.cast_uninit();
let spare_len = self.buf.capacity() - self.len;

// SAFETY:
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/iter/adapters/map_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<T, const N: usize> Buffer<T, N> {

// SAFETY: the index is valid and this is element `a` in the
// diagram above and has not been dropped yet.
unsafe { ptr::drop_in_place(to_drop.cast::<T>()) };
unsafe { ptr::drop_in_place(to_drop.cast_init()) };
}
}

Expand Down
22 changes: 22 additions & 0 deletions library/core/src/ptr/const_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,6 +1430,28 @@ impl<T: PointeeSized> *const T {
}
}

impl<T> *const T {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the tracking issue, since MaybeUninit only supports Sized types, we need a separate block because <T: PointeeSized> is still too lax for MaybeUninit.

/// 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.
///
Expand Down
25 changes: 25 additions & 0 deletions library/core/src/ptr/mut_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
22 changes: 22 additions & 0 deletions library/core/src/ptr/non_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@
// tidy-alphabetical-start
#![feature(bstr)]
#![feature(bstr_internals)]
#![feature(cast_maybe_uninit)]
#![feature(char_internals)]
#![feature(clone_to_uninit)]
#![feature(core_intrinsics)]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
};
unsafe {
let ptr = header.PathBuffer.as_mut_ptr();
ptr.copy_from(abs_path.as_ptr().cast::<MaybeUninit<u16>>(), abs_path.len());
ptr.copy_from(abs_path.as_ptr().cast_uninit(), abs_path.len());

let mut ret = 0;
cvt(c::DeviceIoControl(
Expand Down
Loading