diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 2af242d4b5071..eb99e32343773 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -112,6 +112,7 @@ // Library features: // tidy-alphabetical-start #![cfg_attr(not(bootstrap), feature(offset_of_nested))] +#![feature(array_ptr_get)] #![feature(char_indices_offset)] #![feature(const_align_of_val)] #![feature(const_align_of_val_raw)] diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 5ce9ddeb676a6..031af50264097 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1768,6 +1768,47 @@ impl *const [T] { } } +impl *const [T; N] { + /// Returns a raw pointer to the array's buffer. + /// + /// This is equivalent to casting `self` to `*const T`, but more type-safe. + /// + /// # Examples + /// + /// ```rust + /// #![feature(array_ptr_get)] + /// use std::ptr; + /// + /// let arr: *const [i8; 3] = ptr::null(); + /// assert_eq!(arr.as_ptr(), ptr::null()); + /// ``` + #[inline] + #[unstable(feature = "array_ptr_get", issue = "119411")] + #[rustc_const_unstable(feature = "array_ptr_get", issue = "119411")] + pub const fn as_ptr(self) -> *const T { + self as *const T + } + + /// Returns a raw pointer to a shared slice containing the entire array. + /// + /// # Examples + /// + /// ``` + /// #![feature(array_ptr_get, slice_ptr_len)] + /// + /// let arr: *const [i32; 3] = &[1, 2, 4] as *const [i32; 3]; + /// let slice: *const [i32] = arr.as_slice(); + /// assert_eq!(slice.len(), 3); + /// ``` + #[inline] + #[unstable(feature = "array_ptr_get", issue = "119411")] + #[rustc_const_unstable(feature = "array_ptr_get", issue = "119411")] + pub const fn as_slice(self) -> *const [T] { + // SAFETY: `N` is the length of the array, so the pointer is always in-bounds. + unsafe { slice::from_raw_parts(self.as_ptr(), N) } + } +} + // Equality for pointers #[stable(feature = "rust1", since = "1.0.0")] impl PartialEq for *const T { diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 3e5678a7d9172..f740e4d57e1cb 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -2195,6 +2195,48 @@ impl *mut [T] { } } +impl *mut [T; N] { + /// Returns a raw pointer to the array's buffer. + /// + /// This is equivalent to casting `self` to `*const T`, but more type-safe. + /// + /// # Examples + /// + /// ```rust + /// #![feature(array_ptr_get)] + /// use std::ptr; + /// + /// let arr: *mut [i8; 3] = ptr::null_mut(); + /// assert_eq!(arr.as_mut_ptr(), ptr::null_mut()); + /// ``` + #[inline] + #[unstable(feature = "array_ptr_get", issue = "119411")] + pub fn as_mut_ptr(self) -> *mut T { + self as *mut T + } + + /// Returns a raw pointer to a unique slice containing the entire array. + /// + /// # Examples + /// + /// ``` + /// #![feature(array_ptr_get)] + /// + /// let mut arr = [1, 2, 5]; + /// let ptr: *mut [i32; 3] = &mut arr; + /// unsafe { + /// (&mut *ptr.as_mut_slice())[..2].copy_from_slice(&[3, 4]); + /// } + /// assert_eq!(arr, [3, 4, 5]); + /// ``` + #[inline] + #[unstable(feature = "array_ptr_get", issue = "119411")] + pub fn as_mut_slice(self) -> *mut [T] { + // SAFETY: `N` is the length of the array, so the pointer is always in-bounds. + unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), N) } + } +} + // Equality for pointers #[stable(feature = "rust1", since = "1.0.0")] impl PartialEq for *mut T {