From 382d370c4f5750900ec8896b3659d7624fe048a0 Mon Sep 17 00:00:00 2001 From: Dodo Date: Fri, 20 Dec 2019 21:42:03 +0100 Subject: [PATCH] Make ptr::slice_from_raw_parts a const fn available under a feature flag --- src/libcore/ptr/mod.rs | 6 ++++-- src/libcore/tests/lib.rs | 3 +++ src/libcore/tests/ptr.rs | 11 +++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/libcore/ptr/mod.rs b/src/libcore/ptr/mod.rs index 776165e7bd70c..67a64b53b0be6 100644 --- a/src/libcore/ptr/mod.rs +++ b/src/libcore/ptr/mod.rs @@ -257,7 +257,8 @@ pub(crate) struct FatPtr { /// ``` #[inline] #[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")] -pub fn slice_from_raw_parts(data: *const T, len: usize) -> *const [T] { +#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")] +pub const fn slice_from_raw_parts(data: *const T, len: usize) -> *const [T] { unsafe { Repr { raw: FatPtr { data, len } }.rust } } @@ -273,7 +274,8 @@ pub fn slice_from_raw_parts(data: *const T, len: usize) -> *const [T] { /// [`from_raw_parts_mut`]: ../../std/slice/fn.from_raw_parts_mut.html #[inline] #[unstable(feature = "slice_from_raw_parts", reason = "recently added", issue = "36925")] -pub fn slice_from_raw_parts_mut(data: *mut T, len: usize) -> *mut [T] { +#[rustc_const_unstable(feature = "const_slice_from_raw_parts", issue = "67456")] +pub const fn slice_from_raw_parts_mut(data: *mut T, len: usize) -> *mut [T] { unsafe { Repr { raw: FatPtr { data, len } }.rust_mut } } diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs index b28ed2eaa0876..1f20ebc01e993 100644 --- a/src/libcore/tests/lib.rs +++ b/src/libcore/tests/lib.rs @@ -36,6 +36,9 @@ #![feature(iter_is_partitioned)] #![feature(iter_order_by)] #![feature(cmp_min_max_by)] +#![feature(slice_from_raw_parts)] +#![feature(const_slice_from_raw_parts)] +#![feature(const_raw_ptr_deref)] extern crate test; diff --git a/src/libcore/tests/ptr.rs b/src/libcore/tests/ptr.rs index eea736bc88f01..473bc881d2932 100644 --- a/src/libcore/tests/ptr.rs +++ b/src/libcore/tests/ptr.rs @@ -1,6 +1,17 @@ use core::cell::RefCell; use core::ptr::*; +#[test] +fn test_const_from_raw_parts() { + const SLICE: &[u8] = &[1, 2, 3, 4]; + const FROM_RAW: &[u8] = unsafe { &*slice_from_raw_parts(SLICE.as_ptr(), SLICE.len()) }; + assert_eq!(SLICE, FROM_RAW); + + let slice = &[1, 2, 3, 4, 5]; + let from_raw = unsafe { &*slice_from_raw_parts(slice.as_ptr(), 2) } ; + assert_eq!(&slice[..2], from_raw); +} + #[test] fn test() { unsafe {