Skip to content

Commit

Permalink
Rollup merge of rust-lang#60667 - oli-obk:raw_from_raw_parts, r=sfackler
Browse files Browse the repository at this point in the history
 Add functions for building raw slices to libcore

implement rust-lang#36925
  • Loading branch information
Centril authored Jun 19, 2019
2 parents 9cb052a + 8b21b07 commit 3f34659
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
47 changes: 47 additions & 0 deletions src/libcore/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
19 changes: 3 additions & 16 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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).
Expand Down

0 comments on commit 3f34659

Please sign in to comment.