@@ -2,7 +2,9 @@ use crate::any::type_name;
22use crate :: fmt;
33use crate :: intrinsics;
44use crate :: mem:: ManuallyDrop ;
5+ use crate :: ops:: { Index , IndexMut } ;
56use crate :: ptr;
7+ use crate :: slice:: IterMut ;
68
79/// A wrapper type to construct uninitialized instances of `T`.
810///
@@ -1158,3 +1160,55 @@ impl<T> MaybeUninit<T> {
11581160 unsafe { MaybeUninit :: slice_assume_init_mut ( this) }
11591161 }
11601162}
1163+
1164+ impl < T , const N : usize > Index < usize > for MaybeUninit < [ T ; N ] > {
1165+ type Output = MaybeUninit < T > ;
1166+
1167+ #[ inline]
1168+ fn index ( & self , index : usize ) -> & Self :: Output {
1169+ Index :: index ( self . as_ref ( ) , index)
1170+ }
1171+ }
1172+
1173+ impl < T , const N : usize > IndexMut < usize > for MaybeUninit < [ T ; N ] > {
1174+ #[ inline]
1175+ fn index_mut ( & mut self , index : usize ) -> & mut Self :: Output {
1176+ IndexMut :: index_mut ( self . as_mut ( ) , index)
1177+ }
1178+ }
1179+
1180+ impl < T , const N : usize > AsRef < [ MaybeUninit < T > ] > for MaybeUninit < [ T ; N ] > {
1181+ #[ inline]
1182+ fn as_ref ( & self ) -> & [ MaybeUninit < T > ] {
1183+ let data = self . as_ptr ( ) . cast :: < MaybeUninit < T > > ( ) ;
1184+ // SAFETY: MaybeUninit<[T; N]> and [MaybeUninit<T>; N] have the same layout
1185+ // data points to N consecutive properly initialized values of type MaybeUninit<T>.
1186+ unsafe { crate :: slice:: from_raw_parts ( data, N ) }
1187+ }
1188+ }
1189+
1190+ impl < T , const N : usize > AsMut < [ MaybeUninit < T > ] > for MaybeUninit < [ T ; N ] > {
1191+ #[ inline]
1192+ fn as_mut ( & mut self ) -> & mut [ MaybeUninit < T > ] {
1193+ let data = self . as_mut_ptr ( ) . cast :: < MaybeUninit < T > > ( ) ;
1194+ // SAFETY: MaybeUninit<[T; N]> and [MaybeUninit<T>; N] have the same layout
1195+ // data points to N consecutive properly initialized values of type MaybeUninit<T>.
1196+ unsafe { crate :: slice:: from_raw_parts_mut ( data, N ) }
1197+ }
1198+ }
1199+
1200+ impl < ' a , T , const N : usize > IntoIterator for & ' a mut MaybeUninit < [ T ; N ] > {
1201+ type Item = & ' a mut MaybeUninit < T > ;
1202+ type IntoIter = IterMut < ' a , MaybeUninit < T > > ;
1203+
1204+ fn into_iter ( self ) -> IterMut < ' a , MaybeUninit < T > > {
1205+ self . as_mut ( ) . iter_mut ( )
1206+ }
1207+ }
1208+
1209+ #[ unstable( feature = "maybe_uninit_array_index" , issue = "none" ) ]
1210+ impl < T , const N : usize > MaybeUninit < [ T ; N ] > {
1211+ pub fn iter_mut ( & mut self ) -> IterMut < ' _ , MaybeUninit < T > > {
1212+ self . as_mut ( ) . iter_mut ( )
1213+ }
1214+ }
0 commit comments