Skip to content

Commit 8cb78fe

Browse files
authored
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: #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.
2 parents c4e82ab + d6945f6 commit 8cb78fe

File tree

8 files changed

+74
-3
lines changed

8 files changed

+74
-3
lines changed

library/alloc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
#![feature(async_iterator)]
103103
#![feature(bstr)]
104104
#![feature(bstr_internals)]
105+
#![feature(cast_maybe_uninit)]
105106
#![feature(char_internals)]
106107
#![feature(char_max_len)]
107108
#![feature(clone_to_uninit)]

library/alloc/src/vec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3176,7 +3176,7 @@ impl<T, A: Allocator> Vec<T, A> {
31763176
// - but the allocation extends out to `self.buf.capacity()` elements, possibly
31773177
// uninitialized
31783178
let spare_ptr = unsafe { ptr.add(self.len) };
3179-
let spare_ptr = spare_ptr.cast::<MaybeUninit<T>>();
3179+
let spare_ptr = spare_ptr.cast_uninit();
31803180
let spare_len = self.buf.capacity() - self.len;
31813181

31823182
// SAFETY:

library/core/src/iter/adapters/map_windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<T, const N: usize> Buffer<T, N> {
195195

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

library/core/src/ptr/const_ptr.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,28 @@ impl<T: PointeeSized> *const T {
14301430
}
14311431
}
14321432

1433+
impl<T> *const T {
1434+
/// Casts from a type to its maybe-uninitialized version.
1435+
#[must_use]
1436+
#[inline(always)]
1437+
#[unstable(feature = "cast_maybe_uninit", issue = "145036")]
1438+
pub const fn cast_uninit(self) -> *const MaybeUninit<T> {
1439+
self as _
1440+
}
1441+
}
1442+
impl<T> *const MaybeUninit<T> {
1443+
/// Casts from a maybe-uninitialized type to its initialized version.
1444+
///
1445+
/// This is always safe, since UB can only occur if the pointer is read
1446+
/// before being initialized.
1447+
#[must_use]
1448+
#[inline(always)]
1449+
#[unstable(feature = "cast_maybe_uninit", issue = "145036")]
1450+
pub const fn cast_init(self) -> *const T {
1451+
self as _
1452+
}
1453+
}
1454+
14331455
impl<T> *const [T] {
14341456
/// Returns the length of a raw slice.
14351457
///

library/core/src/ptr/mut_ptr.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,6 +1687,31 @@ impl<T: PointeeSized> *mut T {
16871687
}
16881688
}
16891689

1690+
impl<T> *mut T {
1691+
/// Casts from a type to its maybe-uninitialized version.
1692+
///
1693+
/// This is always safe, since UB can only occur if the pointer is read
1694+
/// before being initialized.
1695+
#[must_use]
1696+
#[inline(always)]
1697+
#[unstable(feature = "cast_maybe_uninit", issue = "145036")]
1698+
pub const fn cast_uninit(self) -> *mut MaybeUninit<T> {
1699+
self as _
1700+
}
1701+
}
1702+
impl<T> *mut MaybeUninit<T> {
1703+
/// Casts from a maybe-uninitialized type to its initialized version.
1704+
///
1705+
/// This is always safe, since UB can only occur if the pointer is read
1706+
/// before being initialized.
1707+
#[must_use]
1708+
#[inline(always)]
1709+
#[unstable(feature = "cast_maybe_uninit", issue = "145036")]
1710+
pub const fn cast_init(self) -> *mut T {
1711+
self as _
1712+
}
1713+
}
1714+
16901715
impl<T> *mut [T] {
16911716
/// Returns the length of a raw slice.
16921717
///

library/core/src/ptr/non_null.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,28 @@ impl<T: PointeeSized> NonNull<T> {
13571357
}
13581358
}
13591359

1360+
impl<T> NonNull<T> {
1361+
/// Casts from a type to its maybe-uninitialized version.
1362+
#[must_use]
1363+
#[inline(always)]
1364+
#[unstable(feature = "cast_maybe_uninit", issue = "145036")]
1365+
pub const fn cast_uninit(self) -> NonNull<MaybeUninit<T>> {
1366+
self.cast()
1367+
}
1368+
}
1369+
impl<T> NonNull<MaybeUninit<T>> {
1370+
/// Casts from a maybe-uninitialized type to its initialized version.
1371+
///
1372+
/// This is always safe, since UB can only occur if the pointer is read
1373+
/// before being initialized.
1374+
#[must_use]
1375+
#[inline(always)]
1376+
#[unstable(feature = "cast_maybe_uninit", issue = "145036")]
1377+
pub const fn cast_init(self) -> NonNull<T> {
1378+
self.cast()
1379+
}
1380+
}
1381+
13601382
impl<T> NonNull<[T]> {
13611383
/// Creates a non-null raw slice from a thin pointer and a length.
13621384
///

library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@
329329
// tidy-alphabetical-start
330330
#![feature(bstr)]
331331
#![feature(bstr_internals)]
332+
#![feature(cast_maybe_uninit)]
332333
#![feature(char_internals)]
333334
#![feature(clone_to_uninit)]
334335
#![feature(const_cmp)]

library/std/src/sys/fs/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ pub fn junction_point(original: &Path, link: &Path) -> io::Result<()> {
16061606
};
16071607
unsafe {
16081608
let ptr = header.PathBuffer.as_mut_ptr();
1609-
ptr.copy_from(abs_path.as_ptr().cast::<MaybeUninit<u16>>(), abs_path.len());
1609+
ptr.copy_from(abs_path.as_ptr().cast_uninit(), abs_path.len());
16101610

16111611
let mut ret = 0;
16121612
cvt(c::DeviceIoControl(

0 commit comments

Comments
 (0)