about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorElichai Turkel <elichai.turkel@gmail.com>2020-04-22 12:39:29 +0300
committerElichai Turkel <elichai.turkel@gmail.com>2020-04-26 15:42:39 +0300
commit6f31f05aaf8e89f87e01052ba61fa04613b0a090 (patch)
tree140350e4d71ceab9a495779ec1d55261b2a7a3d9 /src/liballoc
parent2dc5b602eee35d70e8e6e506a7ea07b6c7e0197d (diff)
downloadrust-6f31f05aaf8e89f87e01052ba61fa04613b0a090.tar.gz
rust-6f31f05aaf8e89f87e01052ba61fa04613b0a090.zip
Add a function to turn Box<T> into Box<[T]> (into_boxed_slice)
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/boxed.rs10
1 files changed, 10 insertions, 0 deletions
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<T> Box<T> {
     pub fn pin(x: T) -> Pin<Box<T>> {
         (box x).into()
     }
+
+    /// Converts a `Box<T>` 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<T>) -> 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<T> Box<[T]> {