@@ -3992,6 +3992,88 @@ impl<T> [T] {
3992
3992
}
3993
3993
}
3994
3994
3995
+ #[ cfg( not( bootstrap) ) ]
3996
+ impl < T , const N : usize > [ [ T ; N ] ] {
3997
+ /// Takes a `&[[T; N]]`, and flattens it to a `&[T]`.
3998
+ ///
3999
+ /// # Panics
4000
+ ///
4001
+ /// This panics if the length of the resulting slice would overflow a `usize`.
4002
+ ///
4003
+ /// This is only possible when flattening a slice of arrays of zero-sized
4004
+ /// types, and thus tends to be irrelevant in practice. If
4005
+ /// `size_of::<T>() > 0`, this will never panic.
4006
+ ///
4007
+ /// # Examples
4008
+ ///
4009
+ /// ```
4010
+ /// #![feature(slice_flatten)]
4011
+ ///
4012
+ /// assert_eq!([[1, 2, 3], [4, 5, 6]].flatten(), &[1, 2, 3, 4, 5, 6]);
4013
+ ///
4014
+ /// assert_eq!(
4015
+ /// [[1, 2, 3], [4, 5, 6]].flatten(),
4016
+ /// [[1, 2], [3, 4], [5, 6]].flatten(),
4017
+ /// );
4018
+ ///
4019
+ /// let slice_of_empty_arrays: &[[i32; 0]] = &[[], [], [], [], []];
4020
+ /// assert!(slice_of_empty_arrays.flatten().is_empty());
4021
+ ///
4022
+ /// let empty_slice_of_arrays: &[[u32; 10]] = &[];
4023
+ /// assert!(empty_slice_of_arrays.flatten().is_empty());
4024
+ /// ```
4025
+ #[ unstable( feature = "slice_flatten" , issue = "95629" ) ]
4026
+ pub fn flatten ( & self ) -> & [ T ] {
4027
+ let len = if crate :: mem:: size_of :: < T > ( ) == 0 {
4028
+ self . len ( ) . checked_mul ( N ) . expect ( "slice len overflow" )
4029
+ } else {
4030
+ // SAFETY: `self.len() * N` cannot overflow because `self` is
4031
+ // already in the address space.
4032
+ unsafe { self . len ( ) . unchecked_mul ( N ) }
4033
+ } ;
4034
+ // SAFETY: `[T]` is layout-identical to `[T; N]`
4035
+ unsafe { from_raw_parts ( self . as_ptr ( ) . cast ( ) , len) }
4036
+ }
4037
+
4038
+ /// Takes a `&mut [[T; N]]`, and flattens it to a `&mut [T]`.
4039
+ ///
4040
+ /// # Panics
4041
+ ///
4042
+ /// This panics if the length of the resulting slice would overflow a `usize`.
4043
+ ///
4044
+ /// This is only possible when flattening a slice of arrays of zero-sized
4045
+ /// types, and thus tends to be irrelevant in practice. If
4046
+ /// `size_of::<T>() > 0`, this will never panic.
4047
+ ///
4048
+ /// # Examples
4049
+ ///
4050
+ /// ```
4051
+ /// #![feature(slice_flatten)]
4052
+ ///
4053
+ /// fn add_5_to_all(slice: &mut [i32]) {
4054
+ /// for i in slice {
4055
+ /// *i += 5;
4056
+ /// }
4057
+ /// }
4058
+ ///
4059
+ /// let mut array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
4060
+ /// add_5_to_all(array.flatten_mut());
4061
+ /// assert_eq!(array, [[6, 7, 8], [9, 10, 11], [12, 13, 14]]);
4062
+ /// ```
4063
+ #[ unstable( feature = "slice_flatten" , issue = "95629" ) ]
4064
+ pub fn flatten_mut ( & mut self ) -> & mut [ T ] {
4065
+ let len = if crate :: mem:: size_of :: < T > ( ) == 0 {
4066
+ self . len ( ) . checked_mul ( N ) . expect ( "slice len overflow" )
4067
+ } else {
4068
+ // SAFETY: `self.len() * N` cannot overflow because `self` is
4069
+ // already in the address space.
4070
+ unsafe { self . len ( ) . unchecked_mul ( N ) }
4071
+ } ;
4072
+ // SAFETY: `[T]` is layout-identical to `[T; N]`
4073
+ unsafe { from_raw_parts_mut ( self . as_mut_ptr ( ) . cast ( ) , len) }
4074
+ }
4075
+ }
4076
+
3995
4077
trait CloneFromSpec < T > {
3996
4078
fn spec_clone_from ( & mut self , src : & [ T ] ) ;
3997
4079
}
0 commit comments