Skip to content

Commit

Permalink
Reduce pointer casts in Box::into_boxed_slice
Browse files Browse the repository at this point in the history
We only need to cast the pointer once to change `Box<T>` to an array
`Box<[T; 1]>`, then we can let unsized coercion return `Box<[T]>`.
  • Loading branch information
cuviper committed Jun 17, 2020
1 parent 2935d29 commit a7c2cf8
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl<T> Box<T> {
#[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]) }
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut [T; 1]) }
}
}

Expand Down

0 comments on commit a7c2cf8

Please sign in to comment.