Skip to content

Commit 8431811

Browse files
committed
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.
1 parent 8382f39 commit 8431811

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/liballoc/slice.rs

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ pub use core::slice::{SplitN, RSplitN, SplitNMut, RSplitNMut};
119119
pub use core::slice::{RSplit, RSplitMut};
120120
#[stable(feature = "rust1", since = "1.0.0")]
121121
pub use core::slice::{from_raw_parts, from_raw_parts_mut};
122+
#[stable(feature = "from_ref", since = "1.22.0")]
123+
pub use core::slice::{from_ref, from_ref_mut};
122124
#[unstable(feature = "slice_get_slice", issue = "35729")]
123125
pub use core::slice::SliceIndex;
124126

src/libcore/slice/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -2450,6 +2450,22 @@ pub unsafe fn from_raw_parts_mut<'a, T>(p: *mut T, len: usize) -> &'a mut [T] {
24502450
mem::transmute(Repr { data: p, len: len })
24512451
}
24522452

2453+
/// Converts a reference to T into a slice of length 1 (without copying).
2454+
#[stable(feature = "from_ref", since = "1.22.0")]
2455+
pub fn from_ref<T>(s: &T) -> &[T] {
2456+
unsafe {
2457+
from_raw_parts(s, 1)
2458+
}
2459+
}
2460+
2461+
/// Converts a reference to T into a slice of length 1 (without copying).
2462+
#[stable(feature = "from_ref", since = "1.22.0")]
2463+
pub fn from_ref_mut<T>(s: &mut T) -> &mut [T] {
2464+
unsafe {
2465+
from_raw_parts_mut(s, 1)
2466+
}
2467+
}
2468+
24532469
// This function is public only because there is no other way to unit test heapsort.
24542470
#[unstable(feature = "sort_internals", reason = "internal to sort module", issue = "0")]
24552471
#[doc(hidden)]

0 commit comments

Comments
 (0)