diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 031af50264097..75bc97e36824d 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1783,8 +1783,8 @@ impl *const [T; N] { /// assert_eq!(arr.as_ptr(), ptr::null()); /// ``` #[inline] - #[unstable(feature = "array_ptr_get", issue = "119411")] - #[rustc_const_unstable(feature = "array_ptr_get", issue = "119411")] + #[unstable(feature = "array_ptr_get", issue = "119834")] + #[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")] pub const fn as_ptr(self) -> *const T { self as *const T } @@ -1801,11 +1801,10 @@ impl *const [T; N] { /// assert_eq!(slice.len(), 3); /// ``` #[inline] - #[unstable(feature = "array_ptr_get", issue = "119411")] - #[rustc_const_unstable(feature = "array_ptr_get", issue = "119411")] + #[unstable(feature = "array_ptr_get", issue = "119834")] + #[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")] 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) } + slice_from_raw_parts(self.as_ptr(), N) } } diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index f740e4d57e1cb..492389f9fa93b 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -2198,7 +2198,7 @@ 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. + /// This is equivalent to casting `self` to `*mut T`, but more type-safe. /// /// # Examples /// @@ -2210,12 +2210,13 @@ impl *mut [T; N] { /// 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 { + #[unstable(feature = "array_ptr_get", issue = "119834")] + #[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")] + pub const fn as_mut_ptr(self) -> *mut T { self as *mut T } - /// Returns a raw pointer to a unique slice containing the entire array. + /// Returns a raw pointer to a mutable slice containing the entire array. /// /// # Examples /// @@ -2230,10 +2231,10 @@ impl *mut [T; N] { /// 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) } + #[unstable(feature = "array_ptr_get", issue = "119834")] + #[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")] + pub const fn as_mut_slice(self) -> *mut [T] { + slice_from_raw_parts_mut(self.as_mut_ptr(), N) } } diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 9a3b477c2d159..2bad5fb1472a3 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -1,5 +1,6 @@ #![feature(alloc_layout_extra)] #![feature(array_chunks)] +#![feature(array_ptr_get)] #![feature(array_windows)] #![feature(ascii_char)] #![feature(ascii_char_variants)] @@ -49,6 +50,7 @@ #![feature(sort_internals)] #![feature(slice_take)] #![feature(slice_from_ptr_range)] +#![feature(slice_ptr_len)] #![feature(slice_split_once)] #![feature(split_as_slice)] #![feature(maybe_uninit_uninit_array)] diff --git a/library/core/tests/ptr.rs b/library/core/tests/ptr.rs index b68f2a50b3211..1ee5610da51e1 100644 --- a/library/core/tests/ptr.rs +++ b/library/core/tests/ptr.rs @@ -1142,3 +1142,16 @@ fn test_const_copy() { assert!(*ptr2 == 1); }; } + +#[test] +fn test_null_array_as_slice() { + let arr: *mut [u8; 4] = null_mut(); + let ptr: *mut [u8] = arr.as_mut_slice(); + assert!(ptr.is_null()); + assert_eq!(ptr.len(), 4); + + let arr: *const [u8; 4] = null(); + let ptr: *const [u8] = arr.as_slice(); + assert!(ptr.is_null()); + assert_eq!(ptr.len(), 4); +}