Skip to content

Commit 3f34659

Browse files
authored
Rollup merge of rust-lang#60667 - oli-obk:raw_from_raw_parts, r=sfackler
Add functions for building raw slices to libcore implement rust-lang#36925
2 parents 9cb052a + 8b21b07 commit 3f34659

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

src/libcore/ptr/mod.rs

+47
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,53 @@ pub const fn null<T>() -> *const T { 0 as *const T }
230230
#[rustc_promotable]
231231
pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
232232

233+
#[repr(C)]
234+
pub(crate) union Repr<T> {
235+
pub(crate) rust: *const [T],
236+
rust_mut: *mut [T],
237+
pub(crate) raw: FatPtr<T>,
238+
}
239+
240+
#[repr(C)]
241+
pub(crate) struct FatPtr<T> {
242+
data: *const T,
243+
pub(crate) len: usize,
244+
}
245+
246+
/// Forms a slice from a pointer and a length.
247+
///
248+
/// The `len` argument is the number of **elements**, not the number of bytes.
249+
///
250+
/// # Examples
251+
///
252+
/// ```rust
253+
/// #![feature(slice_from_raw_parts)]
254+
/// use std::ptr;
255+
///
256+
/// // create a slice pointer when starting out with a pointer to the first element
257+
/// let mut x = [5, 6, 7];
258+
/// let ptr = &mut x[0] as *mut _;
259+
/// let slice = ptr::slice_from_raw_parts_mut(ptr, 3);
260+
/// assert_eq!(unsafe { &*slice }[2], 7);
261+
/// ```
262+
#[inline]
263+
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
264+
pub fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
265+
unsafe { Repr { raw: FatPtr { data, len } }.rust }
266+
}
267+
268+
/// Performs the same functionality as [`from_raw_parts`], except that a
269+
/// mutable slice is returned.
270+
///
271+
/// See the documentation of [`from_raw_parts`] for more details.
272+
///
273+
/// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
274+
#[inline]
275+
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
276+
pub fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
277+
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
278+
}
279+
233280
/// Swaps the values at two mutable locations of the same type, without
234281
/// deinitializing either.
235282
///

src/libcore/slice/mod.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,6 @@ pub mod memchr;
4545
mod rotate;
4646
mod sort;
4747

48-
#[repr(C)]
49-
union Repr<'a, T: 'a> {
50-
rust: &'a [T],
51-
rust_mut: &'a mut [T],
52-
raw: FatPtr<T>,
53-
}
54-
55-
#[repr(C)]
56-
struct FatPtr<T> {
57-
data: *const T,
58-
len: usize,
59-
}
60-
6148
//
6249
// Extension traits
6350
//
@@ -78,7 +65,7 @@ impl<T> [T] {
7865
#[rustc_const_unstable(feature = "const_slice_len")]
7966
pub const fn len(&self) -> usize {
8067
unsafe {
81-
Repr { rust: self }.raw.len
68+
crate::ptr::Repr { rust: self }.raw.len
8269
}
8370
}
8471

@@ -5195,7 +5182,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
51955182
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
51965183
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
51975184
"attempt to create slice covering half the address space");
5198-
Repr { raw: FatPtr { data, len } }.rust
5185+
&*ptr::slice_from_raw_parts(data, len)
51995186
}
52005187

52015188
/// Performs the same functionality as [`from_raw_parts`], except that a
@@ -5216,7 +5203,7 @@ pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]
52165203
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
52175204
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
52185205
"attempt to create slice covering half the address space");
5219-
Repr { raw: FatPtr { data, len } }.rust_mut
5206+
&mut *ptr::slice_from_raw_parts_mut(data, len)
52205207
}
52215208

52225209
/// Converts a reference to T into a slice of length 1 (without copying).

0 commit comments

Comments
 (0)