@@ -1006,26 +1006,6 @@ impl<T> Box<[T]> {
10061006 } ;
10071007 unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, Global ) . into_box ( len) ) }
10081008 }
1009-
1010- /// Converts the boxed slice into a boxed array.
1011- ///
1012- /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
1013- ///
1014- /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1015- #[ unstable( feature = "alloc_slice_into_array" , issue = "148082" ) ]
1016- #[ inline]
1017- #[ must_use]
1018- pub fn into_array < const N : usize > ( self ) -> Option < Box < [ T ; N ] > > {
1019- if self . len ( ) == N {
1020- let ptr = Self :: into_raw ( self ) as * mut [ T ; N ] ;
1021-
1022- // SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
1023- let me = unsafe { Box :: from_raw ( ptr) } ;
1024- Some ( me)
1025- } else {
1026- None
1027- }
1028- }
10291009}
10301010
10311011impl < T , A : Allocator > Box < [ T ] , A > {
@@ -1157,6 +1137,36 @@ impl<T, A: Allocator> Box<[T], A> {
11571137 } ;
11581138 unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, alloc) . into_box ( len) ) }
11591139 }
1140+
1141+ /// Converts the boxed slice into a boxed array.
1142+ ///
1143+ /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
1144+ ///
1145+ /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1146+ ///
1147+ /// # Examples
1148+ ///
1149+ /// ```
1150+ /// #![feature(alloc_slice_into_array)]
1151+ /// let box_slice: Box<[i32]> = Box::new([1, 2, 3]);
1152+ ///
1153+ /// let box_array: Box<[i32; 3]> = box_slice.into_array().unwrap();
1154+ /// ```
1155+ #[ unstable( feature = "alloc_slice_into_array" , issue = "148082" ) ]
1156+ #[ inline]
1157+ #[ must_use]
1158+ pub fn into_array < const N : usize > ( self ) -> Option < Box < [ T ; N ] , A > > {
1159+ if self . len ( ) == N {
1160+ let ( ptr, alloc) = Self :: into_raw_with_allocator ( self ) ;
1161+ let ptr = ptr as * mut [ T ; N ] ;
1162+
1163+ // SAFETY: The underlying array of a slice has the exact same layout as an actual array `[T; N]` if `N` is equal to the slice's length.
1164+ let me = unsafe { Box :: from_raw_in ( ptr, alloc) } ;
1165+ Some ( me)
1166+ } else {
1167+ None
1168+ }
1169+ }
11601170}
11611171
11621172impl < T , A : Allocator > Box < mem:: MaybeUninit < T > , A > {
0 commit comments