diff options
| author | Elichai Turkel <elichai.turkel@gmail.com> | 2020-04-22 12:39:29 +0300 |
|---|---|---|
| committer | Elichai Turkel <elichai.turkel@gmail.com> | 2020-04-26 15:42:39 +0300 |
| commit | 6f31f05aaf8e89f87e01052ba61fa04613b0a090 (patch) | |
| tree | 140350e4d71ceab9a495779ec1d55261b2a7a3d9 /src/liballoc | |
| parent | 2dc5b602eee35d70e8e6e506a7ea07b6c7e0197d (diff) | |
| download | rust-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.rs | 10 |
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]> { |
