From 843181172892af2fdb7755e77c39227aba252c83 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 15 Oct 2017 13:15:41 +0000 Subject: [PATCH 1/2] Bring back slice::ref_slice as slice::from_ref. These functions were deprecated and removed in 1.5, but such simple functionality shouldn't require using unsafe code, and it isn't cluttering libstd too much. --- src/liballoc/slice.rs | 2 ++ src/libcore/slice/mod.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs index 2045d5ddd972d..56450d4e08c99 100644 --- a/src/liballoc/slice.rs +++ b/src/liballoc/slice.rs @@ -119,6 +119,8 @@ pub use core::slice::{SplitN, RSplitN, SplitNMut, RSplitNMut}; pub use core::slice::{RSplit, RSplitMut}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::slice::{from_raw_parts, from_raw_parts_mut}; +#[stable(feature = "from_ref", since = "1.22.0")] +pub use core::slice::{from_ref, from_ref_mut}; #[unstable(feature = "slice_get_slice", issue = "35729")] pub use core::slice::SliceIndex; diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index 5039bef631e51..ff9c00cf73d19 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2450,6 +2450,22 @@ pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: usize) -> &'a mut [T] { mem::transmute(Repr { data: p, len: len }) } +/// Converts a reference to T into a slice of length 1 (without copying). +#[stable(feature = "from_ref", since = "1.22.0")] +pub fn from_ref(s: &T) -> &[T] { + unsafe { + from_raw_parts(s, 1) + } +} + +/// Converts a reference to T into a slice of length 1 (without copying). +#[stable(feature = "from_ref", since = "1.22.0")] +pub fn from_ref_mut(s: &mut T) -> &mut [T] { + unsafe { + from_raw_parts_mut(s, 1) + } +} + // This function is public only because there is no other way to unit test heapsort. #[unstable(feature = "sort_internals", reason = "internal to sort module", issue = "0")] #[doc(hidden)] From 1cc88be2ebc67e1f0e7ff57c12dec45ef9abc2ae Mon Sep 17 00:00:00 2001 From: whitequark Date: Wed, 1 Nov 2017 21:56:17 +0000 Subject: [PATCH 2/2] De-stabilize core::slice::{from_ref, from_ref_mut}. --- src/liballoc/lib.rs | 1 + src/liballoc/slice.rs | 2 +- src/libcore/slice/mod.rs | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 0cbfc9e9dacbb..f654a6b5ba471 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -93,6 +93,7 @@ #![feature(dropck_eyepatch)] #![feature(exact_size_is_empty)] #![feature(fmt_internals)] +#![feature(from_ref)] #![feature(fundamental)] #![feature(fused)] #![feature(generic_param_attrs)] diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs index 56450d4e08c99..0c5fec2cf7499 100644 --- a/src/liballoc/slice.rs +++ b/src/liballoc/slice.rs @@ -119,7 +119,7 @@ pub use core::slice::{SplitN, RSplitN, SplitNMut, RSplitNMut}; pub use core::slice::{RSplit, RSplitMut}; #[stable(feature = "rust1", since = "1.0.0")] pub use core::slice::{from_raw_parts, from_raw_parts_mut}; -#[stable(feature = "from_ref", since = "1.22.0")] +#[unstable(feature = "from_ref", issue = "45703")] pub use core::slice::{from_ref, from_ref_mut}; #[unstable(feature = "slice_get_slice", issue = "35729")] pub use core::slice::SliceIndex; diff --git a/src/libcore/slice/mod.rs b/src/libcore/slice/mod.rs index ff9c00cf73d19..57e5ae28664e0 100644 --- a/src/libcore/slice/mod.rs +++ b/src/libcore/slice/mod.rs @@ -2451,7 +2451,7 @@ pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: usize) -> &'a mut [T] { } /// Converts a reference to T into a slice of length 1 (without copying). -#[stable(feature = "from_ref", since = "1.22.0")] +#[unstable(feature = "from_ref", issue = "45703")] pub fn from_ref(s: &T) -> &[T] { unsafe { from_raw_parts(s, 1) @@ -2459,7 +2459,7 @@ pub fn from_ref(s: &T) -> &[T] { } /// Converts a reference to T into a slice of length 1 (without copying). -#[stable(feature = "from_ref", since = "1.22.0")] +#[unstable(feature = "from_ref", issue = "45703")] pub fn from_ref_mut(s: &mut T) -> &mut [T] { unsafe { from_raw_parts_mut(s, 1)