Skip to content

Commit 31d8277

Browse files
Rollup merge of #144515 - scottmcm:ptr_cast_array, r=Mark-Simulacrum
Implement `ptr_cast_array` ACP: rust-lang/libs-team#602 Tracking Issue: #144514
2 parents 4bc1eb7 + 51b0416 commit 31d8277

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,6 +1569,15 @@ impl<T> *const [T] {
15691569
}
15701570
}
15711571

1572+
impl<T> *const T {
1573+
/// Casts from a pointer-to-`T` to a pointer-to-`[T; N]`.
1574+
#[inline]
1575+
#[unstable(feature = "ptr_cast_array", issue = "144514")]
1576+
pub const fn cast_array<const N: usize>(self) -> *const [T; N] {
1577+
self.cast()
1578+
}
1579+
}
1580+
15721581
impl<T, const N: usize> *const [T; N] {
15731582
/// Returns a raw pointer to the array's buffer.
15741583
///

library/core/src/ptr/mut_ptr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,15 @@ impl<T> *mut [T] {
19901990
}
19911991
}
19921992

1993+
impl<T> *mut T {
1994+
/// Casts from a pointer-to-`T` to a pointer-to-`[T; N]`.
1995+
#[inline]
1996+
#[unstable(feature = "ptr_cast_array", issue = "144514")]
1997+
pub const fn cast_array<const N: usize>(self) -> *mut [T; N] {
1998+
self.cast()
1999+
}
2000+
}
2001+
19932002
impl<T, const N: usize> *mut [T; N] {
19942003
/// Returns a raw pointer to the array's buffer.
19952004
///

library/core/src/ptr/non_null.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ impl<T: Sized> NonNull<T> {
193193
// requirements for a reference.
194194
unsafe { &mut *self.cast().as_ptr() }
195195
}
196+
197+
/// Casts from a pointer-to-`T` to a pointer-to-`[T; N]`.
198+
#[inline]
199+
#[unstable(feature = "ptr_cast_array", issue = "144514")]
200+
pub const fn cast_array<const N: usize>(self) -> NonNull<[T; N]> {
201+
self.cast()
202+
}
196203
}
197204

198205
impl<T: PointeeSized> NonNull<T> {

library/core/src/slice/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ impl<T> [T] {
328328
} else {
329329
// SAFETY: We explicitly check for the correct number of elements,
330330
// and do not let the reference outlive the slice.
331-
Some(unsafe { &*(self.as_ptr().cast::<[T; N]>()) })
331+
Some(unsafe { &*(self.as_ptr().cast_array()) })
332332
}
333333
}
334334

@@ -359,7 +359,7 @@ impl<T> [T] {
359359
// SAFETY: We explicitly check for the correct number of elements,
360360
// do not let the reference outlive the slice,
361361
// and require exclusive access to the entire slice to mutate the chunk.
362-
Some(unsafe { &mut *(self.as_mut_ptr().cast::<[T; N]>()) })
362+
Some(unsafe { &mut *(self.as_mut_ptr().cast_array()) })
363363
}
364364
}
365365

@@ -387,7 +387,7 @@ impl<T> [T] {
387387

388388
// SAFETY: We explicitly check for the correct number of elements,
389389
// and do not let the references outlive the slice.
390-
Some((unsafe { &*(first.as_ptr().cast::<[T; N]>()) }, tail))
390+
Some((unsafe { &*(first.as_ptr().cast_array()) }, tail))
391391
}
392392

393393
/// Returns a mutable array reference to the first `N` items in the slice and the remaining
@@ -420,7 +420,7 @@ impl<T> [T] {
420420
// SAFETY: We explicitly check for the correct number of elements,
421421
// do not let the reference outlive the slice,
422422
// and enforce exclusive mutability of the chunk by the split.
423-
Some((unsafe { &mut *(first.as_mut_ptr().cast::<[T; N]>()) }, tail))
423+
Some((unsafe { &mut *(first.as_mut_ptr().cast_array()) }, tail))
424424
}
425425

426426
/// Returns an array reference to the last `N` items in the slice and the remaining slice.
@@ -448,7 +448,7 @@ impl<T> [T] {
448448

449449
// SAFETY: We explicitly check for the correct number of elements,
450450
// and do not let the references outlive the slice.
451-
Some((init, unsafe { &*(last.as_ptr().cast::<[T; N]>()) }))
451+
Some((init, unsafe { &*(last.as_ptr().cast_array()) }))
452452
}
453453

454454
/// Returns a mutable array reference to the last `N` items in the slice and the remaining
@@ -482,7 +482,7 @@ impl<T> [T] {
482482
// SAFETY: We explicitly check for the correct number of elements,
483483
// do not let the reference outlive the slice,
484484
// and enforce exclusive mutability of the chunk by the split.
485-
Some((init, unsafe { &mut *(last.as_mut_ptr().cast::<[T; N]>()) }))
485+
Some((init, unsafe { &mut *(last.as_mut_ptr().cast_array()) }))
486486
}
487487

488488
/// Returns an array reference to the last `N` items in the slice.
@@ -511,7 +511,7 @@ impl<T> [T] {
511511

512512
// SAFETY: We explicitly check for the correct number of elements,
513513
// and do not let the references outlive the slice.
514-
Some(unsafe { &*(last.as_ptr().cast::<[T; N]>()) })
514+
Some(unsafe { &*(last.as_ptr().cast_array()) })
515515
}
516516

517517
/// Returns a mutable array reference to the last `N` items in the slice.
@@ -542,7 +542,7 @@ impl<T> [T] {
542542
// SAFETY: We explicitly check for the correct number of elements,
543543
// do not let the reference outlive the slice,
544544
// and require exclusive access to the entire slice to mutate the chunk.
545-
Some(unsafe { &mut *(last.as_mut_ptr().cast::<[T; N]>()) })
545+
Some(unsafe { &mut *(last.as_mut_ptr().cast_array()) })
546546
}
547547

548548
/// Returns a reference to an element or subslice depending on the type of
@@ -846,7 +846,7 @@ impl<T> [T] {
846846
#[must_use]
847847
pub const fn as_array<const N: usize>(&self) -> Option<&[T; N]> {
848848
if self.len() == N {
849-
let ptr = self.as_ptr() as *const [T; N];
849+
let ptr = self.as_ptr().cast_array();
850850

851851
// SAFETY: The underlying array of a slice can be reinterpreted as an actual array `[T; N]` if `N` is not greater than the slice's length.
852852
let me = unsafe { &*ptr };
@@ -864,7 +864,7 @@ impl<T> [T] {
864864
#[must_use]
865865
pub const fn as_mut_array<const N: usize>(&mut self) -> Option<&mut [T; N]> {
866866
if self.len() == N {
867-
let ptr = self.as_mut_ptr() as *mut [T; N];
867+
let ptr = self.as_mut_ptr().cast_array();
868868

869869
// SAFETY: The underlying array of a slice can be reinterpreted as an actual array `[T; N]` if `N` is not greater than the slice's length.
870870
let me = unsafe { &mut *ptr };

0 commit comments

Comments
 (0)