Skip to content

Commit 82eaa64

Browse files
authored
Unrolled build for rust-lang#119411
Rollup merge of rust-lang#119411 - yotamofek:array-ptr-get, r=Nilstrieb Add as_(mut_)ptr and as_(mut_)slice to raw array pointers Hey, first time contributing to the standard libraries so not completely sure about the process. These functions are complementary to the ones being added in rust-lang#74265 . I found them missing on array pointers. See also: - ACP: rust-lang/libs-team#321 - Tracking issue: rust-lang#119834
2 parents 1eb882e + d0c0887 commit 82eaa64

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
//
113113
// Library features:
114114
// tidy-alphabetical-start
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

+40
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,46 @@ impl<T> *const [T] {
17981798
}
17991799
}
18001800

1801+
impl<T, const N: usize> *const [T; N] {
1802+
/// Returns a raw pointer to the array's buffer.
1803+
///
1804+
/// This is equivalent to casting `self` to `*const T`, but more type-safe.
1805+
///
1806+
/// # Examples
1807+
///
1808+
/// ```rust
1809+
/// #![feature(array_ptr_get)]
1810+
/// use std::ptr;
1811+
///
1812+
/// let arr: *const [i8; 3] = ptr::null();
1813+
/// assert_eq!(arr.as_ptr(), ptr::null());
1814+
/// ```
1815+
#[inline]
1816+
#[unstable(feature = "array_ptr_get", issue = "119834")]
1817+
#[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")]
1818+
pub const fn as_ptr(self) -> *const T {
1819+
self as *const T
1820+
}
1821+
1822+
/// Returns a raw pointer to a slice containing the entire array.
1823+
///
1824+
/// # Examples
1825+
///
1826+
/// ```
1827+
/// #![feature(array_ptr_get, slice_ptr_len)]
1828+
///
1829+
/// let arr: *const [i32; 3] = &[1, 2, 4] as *const [i32; 3];
1830+
/// let slice: *const [i32] = arr.as_slice();
1831+
/// assert_eq!(slice.len(), 3);
1832+
/// ```
1833+
#[inline]
1834+
#[unstable(feature = "array_ptr_get", issue = "119834")]
1835+
#[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")]
1836+
pub const fn as_slice(self) -> *const [T] {
1837+
self
1838+
}
1839+
}
1840+
18011841
// Equality for pointers
18021842
#[stable(feature = "rust1", since = "1.0.0")]
18031843
impl<T: ?Sized> PartialEq for *const T {

library/core/src/ptr/mut_ptr.rs

+43
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,49 @@ impl<T> *mut [T] {
22092209
}
22102210
}
22112211

2212+
impl<T, const N: usize> *mut [T; N] {
2213+
/// Returns a raw pointer to the array's buffer.
2214+
///
2215+
/// This is equivalent to casting `self` to `*mut T`, but more type-safe.
2216+
///
2217+
/// # Examples
2218+
///
2219+
/// ```rust
2220+
/// #![feature(array_ptr_get)]
2221+
/// use std::ptr;
2222+
///
2223+
/// let arr: *mut [i8; 3] = ptr::null_mut();
2224+
/// assert_eq!(arr.as_mut_ptr(), ptr::null_mut());
2225+
/// ```
2226+
#[inline]
2227+
#[unstable(feature = "array_ptr_get", issue = "119834")]
2228+
#[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")]
2229+
pub const fn as_mut_ptr(self) -> *mut T {
2230+
self as *mut T
2231+
}
2232+
2233+
/// Returns a raw pointer to a mutable slice containing the entire array.
2234+
///
2235+
/// # Examples
2236+
///
2237+
/// ```
2238+
/// #![feature(array_ptr_get)]
2239+
///
2240+
/// let mut arr = [1, 2, 5];
2241+
/// let ptr: *mut [i32; 3] = &mut arr;
2242+
/// unsafe {
2243+
/// (&mut *ptr.as_mut_slice())[..2].copy_from_slice(&[3, 4]);
2244+
/// }
2245+
/// assert_eq!(arr, [3, 4, 5]);
2246+
/// ```
2247+
#[inline]
2248+
#[unstable(feature = "array_ptr_get", issue = "119834")]
2249+
#[rustc_const_unstable(feature = "array_ptr_get", issue = "119834")]
2250+
pub const fn as_mut_slice(self) -> *mut [T] {
2251+
self
2252+
}
2253+
}
2254+
22122255
// Equality for pointers
22132256
#[stable(feature = "rust1", since = "1.0.0")]
22142257
impl<T: ?Sized> PartialEq for *mut T {

library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(alloc_layout_extra)]
22
#![feature(array_chunks)]
3+
#![feature(array_ptr_get)]
34
#![feature(array_windows)]
45
#![feature(ascii_char)]
56
#![feature(ascii_char_variants)]
@@ -52,6 +53,7 @@
5253
#![feature(sort_internals)]
5354
#![feature(slice_take)]
5455
#![feature(slice_from_ptr_range)]
56+
#![feature(slice_ptr_len)]
5557
#![feature(slice_split_once)]
5658
#![feature(split_as_slice)]
5759
#![feature(maybe_uninit_fill)]

library/core/tests/ptr.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1141,3 +1141,16 @@ fn test_const_copy() {
11411141
assert!(*ptr2 == 1);
11421142
};
11431143
}
1144+
1145+
#[test]
1146+
fn test_null_array_as_slice() {
1147+
let arr: *mut [u8; 4] = null_mut();
1148+
let ptr: *mut [u8] = arr.as_mut_slice();
1149+
assert!(ptr.is_null());
1150+
assert_eq!(ptr.len(), 4);
1151+
1152+
let arr: *const [u8; 4] = null();
1153+
let ptr: *const [u8] = arr.as_slice();
1154+
assert!(ptr.is_null());
1155+
assert_eq!(ptr.len(), 4);
1156+
}

0 commit comments

Comments
 (0)