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