about summary refs log tree commit diff
diff options
context:
space:
mode:
authorwhitequark <whitequark@whitequark.org>2017-10-15 13:15:41 +0000
committerwhitequark <whitequark@whitequark.org>2017-10-23 22:53:31 +0000
commit843181172892af2fdb7755e77c39227aba252c83 (patch)
tree465f464beea78818a76205764cff03db64d75aef
parent8382f39b6b5820927ccf5380d8240a0c02311645 (diff)
downloadrust-843181172892af2fdb7755e77c39227aba252c83.tar.gz
rust-843181172892af2fdb7755e77c39227aba252c83.zip
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.
-rw-r--r--src/liballoc/slice.rs2
-rw-r--r--src/libcore/slice/mod.rs16
2 files changed, 18 insertions, 0 deletions
diff --git a/src/liballoc/slice.rs b/src/liballoc/slice.rs
index 2045d5ddd97..56450d4e08c 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 5039bef631e..ff9c00cf73d 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<T>(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<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)]