From 6f31f05aaf8e89f87e01052ba61fa04613b0a090 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Wed, 22 Apr 2020 12:39:29 +0300 Subject: Add a function to turn Box into Box<[T]> (into_boxed_slice) --- src/liballoc/boxed.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index db7420954a0..5419491fc23 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -239,6 +239,16 @@ impl Box { pub fn pin(x: T) -> Pin> { (box x).into() } + + /// Converts a `Box` into a `Box<[T]>` + /// + /// This conversion does not allocate on the heap and happens in place. + /// + #[unstable(feature = "box_into_boxed_slice", issue = "71582")] + pub fn into_boxed_slice(boxed: Box) -> Box<[T]> { + // *mut T and *mut [T; 1] have the same size and alignment + unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [T; 1] as *mut [T]) } + } } impl Box<[T]> { -- cgit 1.4.1-3-g733a5