Skip to content

Commit

Permalink
Add a function to turn Box<T> into Box<[T]> (into_boxed_slice)
Browse files Browse the repository at this point in the history
  • Loading branch information
elichai committed Apr 26, 2020
1 parent 2dc5b60 commit 6f31f05
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]> {
Expand Down

0 comments on commit 6f31f05

Please sign in to comment.