Skip to content

Commit 7ead576

Browse files
committed
Implement ptr_cast_array
1 parent a7a1618 commit 7ead576

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

library/core/src/ptr/const_ptr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,15 @@ impl<T> *const [T] {
15471547
}
15481548
}
15491549

1550+
impl<T> *const T {
1551+
/// Casts from a pointer-to-`T` to a pointer-to-`[T; N]`.
1552+
#[inline]
1553+
#[unstable(feature = "ptr_cast_array", issue = "144514")]
1554+
pub const fn cast_array<const N: usize>(self) -> *const [T; N] {
1555+
self.cast()
1556+
}
1557+
}
1558+
15501559
impl<T, const N: usize> *const [T; N] {
15511560
/// Returns a raw pointer to the array's buffer.
15521561
///

library/core/src/ptr/mut_ptr.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,15 @@ impl<T> *mut [T] {
19651965
}
19661966
}
19671967

1968+
impl<T> *mut T {
1969+
/// Casts from a pointer-to-`T` to a pointer-to-`[T; N]`.
1970+
#[inline]
1971+
#[unstable(feature = "ptr_cast_array", issue = "144514")]
1972+
pub const fn cast_array<const N: usize>(self) -> *mut [T; N] {
1973+
self.cast()
1974+
}
1975+
}
1976+
19681977
impl<T, const N: usize> *mut [T; N] {
19691978
/// Returns a raw pointer to the array's buffer.
19701979
///

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> {

0 commit comments

Comments
 (0)