Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fd3942d

Browse files
committedJan 20, 2024
Add as_(mut_)ptr and as_(mut_)slice to raw array pointers
1 parent 227abac commit fd3942d

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
 

‎library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
// Library features:
113113
// tidy-alphabetical-start
114114
#![cfg_attr(not(bootstrap), feature(offset_of_nested))]
115+
#![feature(array_ptr_get)]
115116
#![feature(char_indices_offset)]
116117
#![feature(const_align_of_val)]
117118
#![feature(const_align_of_val_raw)]

‎library/core/src/ptr/const_ptr.rs

+41
Original file line numberDiff line numberDiff line change
@@ -1768,6 +1768,47 @@ impl<T> *const [T] {
17681768
}
17691769
}
17701770

1771+
impl<T, const N: usize> *const [T; N] {
1772+
/// Returns a raw pointer to the array's buffer.
1773+
///
1774+
/// This is equivalent to casting `self` to `*const T`, but more type-safe.
1775+
///
1776+
/// # Examples
1777+
///
1778+
/// ```rust
1779+
/// #![feature(array_ptr_get)]
1780+
/// use std::ptr;
1781+
///
1782+
/// let arr: *const [i8; 3] = ptr::null();
1783+
/// assert_eq!(arr.as_ptr(), ptr::null());
1784+
/// ```
1785+
#[inline]
1786+
#[unstable(feature = "array_ptr_get", issue = "119411")]
1787+
#[rustc_const_unstable(feature = "array_ptr_get", issue = "119411")]
1788+
pub const fn as_ptr(self) -> *const T {
1789+
self as *const T
1790+
}
1791+
1792+
/// Returns a raw pointer to a shared slice containing the entire array.
1793+
///
1794+
/// # Examples
1795+
///
1796+
/// ```
1797+
/// #![feature(array_ptr_get, slice_ptr_len)]
1798+
///
1799+
/// let arr: *const [i32; 3] = &[1, 2, 4] as *const [i32; 3];
1800+
/// let slice: *const [i32] = arr.as_slice();
1801+
/// assert_eq!(slice.len(), 3);
1802+
/// ```
1803+
#[inline]
1804+
#[unstable(feature = "array_ptr_get", issue = "119411")]
1805+
#[rustc_const_unstable(feature = "array_ptr_get", issue = "119411")]
1806+
pub const fn as_slice(self) -> *const [T] {
1807+
// SAFETY: `N` is the length of the array, so the pointer is always in-bounds.
1808+
unsafe { slice::from_raw_parts(self.as_ptr(), N) }
1809+
}
1810+
}
1811+
17711812
// Equality for pointers
17721813
#[stable(feature = "rust1", since = "1.0.0")]
17731814
impl<T: ?Sized> PartialEq for *const T {

‎library/core/src/ptr/mut_ptr.rs

+42
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,48 @@ impl<T> *mut [T] {
21952195
}
21962196
}
21972197

2198+
impl<T, const N: usize> *mut [T; N] {
2199+
/// Returns a raw pointer to the array's buffer.
2200+
///
2201+
/// This is equivalent to casting `self` to `*const T`, but more type-safe.
2202+
///
2203+
/// # Examples
2204+
///
2205+
/// ```rust
2206+
/// #![feature(array_ptr_get)]
2207+
/// use std::ptr;
2208+
///
2209+
/// let arr: *mut [i8; 3] = ptr::null_mut();
2210+
/// assert_eq!(arr.as_mut_ptr(), ptr::null_mut());
2211+
/// ```
2212+
#[inline]
2213+
#[unstable(feature = "array_ptr_get", issue = "119411")]
2214+
pub fn as_mut_ptr(self) -> *mut T {
2215+
self as *mut T
2216+
}
2217+
2218+
/// Returns a raw pointer to a unique slice containing the entire array.
2219+
///
2220+
/// # Examples
2221+
///
2222+
/// ```
2223+
/// #![feature(array_ptr_get)]
2224+
///
2225+
/// let mut arr = [1, 2, 5];
2226+
/// let ptr: *mut [i32; 3] = &mut arr;
2227+
/// unsafe {
2228+
/// (&mut *ptr.as_mut_slice())[..2].copy_from_slice(&[3, 4]);
2229+
/// }
2230+
/// assert_eq!(arr, [3, 4, 5]);
2231+
/// ```
2232+
#[inline]
2233+
#[unstable(feature = "array_ptr_get", issue = "119411")]
2234+
pub fn as_mut_slice(self) -> *mut [T] {
2235+
// SAFETY: `N` is the length of the array, so the pointer is always in-bounds.
2236+
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), N) }
2237+
}
2238+
}
2239+
21982240
// Equality for pointers
21992241
#[stable(feature = "rust1", since = "1.0.0")]
22002242
impl<T: ?Sized> PartialEq for *mut T {

0 commit comments

Comments
 (0)
Please sign in to comment.