Add a function to turn Box<T> into Box<[T]> (into_boxed_slice)

上级 2dc5b602
...@@ -239,6 +239,16 @@ pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> { ...@@ -239,6 +239,16 @@ pub fn new_zeroed() -> Box<mem::MaybeUninit<T>> {
pub fn pin(x: T) -> Pin<Box<T>> { pub fn pin(x: T) -> Pin<Box<T>> {
(box x).into() (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]> { impl<T> Box<[T]> {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册