Skip to content

Commit 6f31f05

Browse files
committed
Add a function to turn Box<T> into Box<[T]> (into_boxed_slice)
1 parent 2dc5b60 commit 6f31f05

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/liballoc/boxed.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ impl<T> Box<T> {
239239
pub fn pin(x: T) -> Pin<Box<T>> {
240240
(box x).into()
241241
}
242+
243+
/// Converts a `Box<T>` into a `Box<[T]>`
244+
///
245+
/// This conversion does not allocate on the heap and happens in place.
246+
///
247+
#[unstable(feature = "box_into_boxed_slice", issue = "71582")]
248+
pub fn into_boxed_slice(boxed: Box<T>) -> Box<[T]> {
249+
// *mut T and *mut [T; 1] have the same size and alignment
250+
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [T; 1] as *mut [T]) }
251+
}
242252
}
243253

244254
impl<T> Box<[T]> {

0 commit comments

Comments
 (0)