]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #45306 - whitequark:ref_slice, r=alexcrichton
authorbors <bors@rust-lang.org>
Thu, 2 Nov 2017 00:06:16 +0000 (00:06 +0000)
committerbors <bors@rust-lang.org>
Thu, 2 Nov 2017 00:06:16 +0000 (00:06 +0000)
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.

The original removal was quite contentious (see #27774), since then
we've had precedent for including such nuggets of functionality (see rust-lang/rfcs#1789),
and @nikomatsakis has provided a lot of use cases in https://github.com/rust-lang/rfcs/pull/1789#issuecomment-314640034.
Hence this PR.

I'm not too sure what to do with stability, feel free to correct me.
It seems pointless to go through stabilization for these functions though.

cc @aturon

src/liballoc/lib.rs
src/liballoc/slice.rs
src/libcore/slice/mod.rs

index 0cbfc9e9dacbb3f04d7f9f30f3c89e0d44d9bf4b..f654a6b5ba4713f0ac01637b99f21cc3499180ef 100644 (file)
@@ -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)]
index 2045d5ddd972ddc37645294b811535bf6974e925..0c5fec2cf74994379e343a3ac649725b89a257ed 100644 (file)
 pub use core::slice::{RSplit, RSplitMut};
 #[stable(feature = "rust1", since = "1.0.0")]
 pub use core::slice::{from_raw_parts, from_raw_parts_mut};
+#[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;
 
index 5039bef631e51257b9dd2b49165da61e785abe2c..57e5ae28664e0ff2aa0841f9f29516cd41effcbe 100644 (file)
@@ -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).
+#[unstable(feature = "from_ref", issue = "45703")]
+pub fn from_ref<T>(s: &T) -> &[T] {
+    unsafe {
+        from_raw_parts(s, 1)
+    }
+}
+
+/// Converts a reference to T into a slice of length 1 (without copying).
+#[unstable(feature = "from_ref", issue = "45703")]
+pub fn from_ref_mut<T>(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)]