Skip to content

Commit d5b69d4

Browse files
committed
Auto merge of #45306 - whitequark:ref_slice, r=alexcrichton
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 rust-lang/rfcs#1789 (comment). 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
2 parents 2be4cc0 + 1cc88be commit d5b69d4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/liballoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#![feature(dropck_eyepatch)]
9494
#![feature(exact_size_is_empty)]
9595
#![feature(fmt_internals)]
96+
#![feature(from_ref)]
9697
#![feature(fundamental)]
9798
#![feature(fused)]
9899
#![feature(generic_param_attrs)]

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+
#[unstable(feature = "from_ref", issue = "45703")]
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+
#[unstable(feature = "from_ref", issue = "45703")]
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+
#[unstable(feature = "from_ref", issue = "45703")]
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)