File tree 3 files changed +60
-0
lines changed
3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -761,6 +761,26 @@ impl<T> Box<[T]> {
761
761
} ;
762
762
unsafe { Ok ( RawVec :: from_raw_parts_in ( ptr. as_ptr ( ) , len, Global ) . into_box ( len) ) }
763
763
}
764
+
765
+ /// Converts the boxed slice into a boxed array.
766
+ ///
767
+ /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
768
+ ///
769
+ /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
770
+ #[ unstable( feature = "slice_as_array" , issue = "133508" ) ]
771
+ #[ inline]
772
+ #[ must_use]
773
+ pub fn into_array < const N : usize > ( self ) -> Option < Box < [ T ; N ] > > {
774
+ if self . len ( ) == N {
775
+ let ptr = Self :: into_raw ( self ) as * mut [ T ; N ] ;
776
+
777
+ // 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.
778
+ let me = unsafe { Box :: from_raw ( ptr) } ;
779
+ Some ( me)
780
+ } else {
781
+ None
782
+ }
783
+ }
764
784
}
765
785
766
786
impl < T , A : Allocator > Box < [ T ] , A > {
Original file line number Diff line number Diff line change @@ -1084,6 +1084,26 @@ impl<T> Rc<[T]> {
1084
1084
) )
1085
1085
}
1086
1086
}
1087
+
1088
+ /// Converts the reference-counted slice into a reference-counted array.
1089
+ ///
1090
+ /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
1091
+ ///
1092
+ /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1093
+ #[ unstable( feature = "slice_as_array" , issue = "133508" ) ]
1094
+ #[ inline]
1095
+ #[ must_use]
1096
+ pub fn into_array < const N : usize > ( self ) -> Option < Rc < [ T ; N ] > > {
1097
+ if self . len ( ) == N {
1098
+ let ptr = Self :: into_raw ( self ) as * const [ T ; N ] ;
1099
+
1100
+ // 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.
1101
+ let me = unsafe { Rc :: from_raw ( ptr) } ;
1102
+ Some ( me)
1103
+ } else {
1104
+ None
1105
+ }
1106
+ }
1087
1107
}
1088
1108
1089
1109
impl < T , A : Allocator > Rc < [ T ] , A > {
Original file line number Diff line number Diff line change @@ -1203,6 +1203,26 @@ impl<T> Arc<[T]> {
1203
1203
) )
1204
1204
}
1205
1205
}
1206
+
1207
+ /// Converts the reference-counted slice into a reference-counted array.
1208
+ ///
1209
+ /// This operation does not reallocate; the underlying array of the slice is simply reinterpreted as an array type.
1210
+ ///
1211
+ /// If `N` is not exactly equal to the length of `self`, then this method returns `None`.
1212
+ #[ unstable( feature = "slice_as_array" , issue = "133508" ) ]
1213
+ #[ inline]
1214
+ #[ must_use]
1215
+ pub fn into_array < const N : usize > ( self ) -> Option < Arc < [ T ; N ] > > {
1216
+ if self . len ( ) == N {
1217
+ let ptr = Self :: into_raw ( self ) as * const [ T ; N ] ;
1218
+
1219
+ // 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.
1220
+ let me = unsafe { Arc :: from_raw ( ptr) } ;
1221
+ Some ( me)
1222
+ } else {
1223
+ None
1224
+ }
1225
+ }
1206
1226
}
1207
1227
1208
1228
impl < T , A : Allocator > Arc < [ T ] , A > {
You can’t perform that action at this time.
0 commit comments