Rollup merge of #60667 - oli-obk:raw_from_raw_parts, r=sfackler
Add functions for building raw slices to libcore implement https://github.com/rust-lang/rust/issues/36925
This commit is contained in:
commit
564326a626
2 changed files with 50 additions and 16 deletions
|
@ -230,6 +230,53 @@ pub const fn null<T>() -> *const T { 0 as *const T }
|
|||
#[rustc_promotable]
|
||||
pub const fn null_mut<T>() -> *mut T { 0 as *mut T }
|
||||
|
||||
#[repr(C)]
|
||||
pub(crate) union Repr<T> {
|
||||
pub(crate) rust: *const [T],
|
||||
rust_mut: *mut [T],
|
||||
pub(crate) raw: FatPtr<T>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub(crate) struct FatPtr<T> {
|
||||
data: *const T,
|
||||
pub(crate) len: usize,
|
||||
}
|
||||
|
||||
/// Forms a slice from a pointer and a length.
|
||||
///
|
||||
/// The `len` argument is the number of **elements**, not the number of bytes.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// #![feature(slice_from_raw_parts)]
|
||||
/// use std::ptr;
|
||||
///
|
||||
/// // create a slice pointer when starting out with a pointer to the first element
|
||||
/// let mut x = [5, 6, 7];
|
||||
/// let ptr = &mut x[0] as *mut _;
|
||||
/// let slice = ptr::slice_from_raw_parts_mut(ptr, 3);
|
||||
/// assert_eq!(unsafe { &*slice }[2], 7);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
|
||||
pub fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T] {
|
||||
unsafe { Repr { raw: FatPtr { data, len } }.rust }
|
||||
}
|
||||
|
||||
/// Performs the same functionality as [`from_raw_parts`], except that a
|
||||
/// mutable slice is returned.
|
||||
///
|
||||
/// See the documentation of [`from_raw_parts`] for more details.
|
||||
///
|
||||
/// [`from_raw_parts`]: ../../std/slice/fn.from_raw_parts.html
|
||||
#[inline]
|
||||
#[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")]
|
||||
pub fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T] {
|
||||
unsafe { Repr { raw: FatPtr { data, len } }.rust_mut }
|
||||
}
|
||||
|
||||
/// Swaps the values at two mutable locations of the same type, without
|
||||
/// deinitializing either.
|
||||
///
|
||||
|
|
|
@ -45,19 +45,6 @@ pub mod memchr;
|
|||
mod rotate;
|
||||
mod sort;
|
||||
|
||||
#[repr(C)]
|
||||
union Repr<'a, T: 'a> {
|
||||
rust: &'a [T],
|
||||
rust_mut: &'a mut [T],
|
||||
raw: FatPtr<T>,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct FatPtr<T> {
|
||||
data: *const T,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
//
|
||||
// Extension traits
|
||||
//
|
||||
|
@ -78,7 +65,7 @@ impl<T> [T] {
|
|||
#[rustc_const_unstable(feature = "const_slice_len")]
|
||||
pub const fn len(&self) -> usize {
|
||||
unsafe {
|
||||
Repr { rust: self }.raw.len
|
||||
crate::ptr::Repr { rust: self }.raw.len
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5195,7 +5182,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
|
|||
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
|
||||
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
|
||||
"attempt to create slice covering half the address space");
|
||||
Repr { raw: FatPtr { data, len } }.rust
|
||||
&*ptr::slice_from_raw_parts(data, len)
|
||||
}
|
||||
|
||||
/// 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]
|
|||
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
|
||||
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
|
||||
"attempt to create slice covering half the address space");
|
||||
Repr { raw: FatPtr { data, len } }.rust_mut
|
||||
&mut *ptr::slice_from_raw_parts_mut(data, len)
|
||||
}
|
||||
|
||||
/// Converts a reference to T into a slice of length 1 (without copying).
|
||||
|
|
Loading…
Reference in a new issue