@@ -11,7 +11,6 @@ use crate::cmp::Ordering;
1111use crate :: convert:: { Infallible , TryFrom } ;
1212use crate :: fmt;
1313use crate :: hash:: { self , Hash } ;
14- use crate :: iter:: FromIterator ;
1514use crate :: marker:: Unsize ;
1615use crate :: mem:: MaybeUninit ;
1716use crate :: slice:: { Iter , IterMut } ;
@@ -176,7 +175,10 @@ impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] {
176175 }
177176}
178177
179- /// Return Error of the FromIterator impl for array
178+ /// The error returned by the `FromIterator` implementation of
179+ /// arrays once these are implemented.
180+ ///
181+ /// Until then `FillError::new().fill(iter)` can be used instead.
180182#[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
181183pub struct FillError < T , const N : usize > {
182184 array : [ MaybeUninit < T > ; N ] ,
@@ -194,27 +196,26 @@ impl<T, const N: usize> fmt::Display for FillError<T, N> {
194196}
195197
196198#[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
197- impl < T : fmt :: Debug , const N : usize > fmt:: Debug for FillError < T , N > {
199+ impl < T , const N : usize > fmt:: Debug for FillError < T , N > {
198200 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
199- f. debug_struct ( "FillError" )
200- . field ( "array" , & self . as_slice ( ) )
201- . field ( "len" , & self . len ( ) )
202- . finish ( )
201+ fmt:: Display :: fmt ( self , f)
203202 }
204203}
205204
206205#[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
207206impl < T , const N : usize > Drop for FillError < T , N > {
208207 fn drop ( & mut self ) {
209208 // SAFETY: This is safe: `as_mut_slice` returns exactly the sub-slice
210- // of elements that have been initialized and need to be droped
209+ // of elements that have been initialized and need to be dropped.
211210 unsafe { crate :: ptr:: drop_in_place ( self . as_mut_slice ( ) ) }
212211 }
213212}
214213
215214#[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
216215impl < T , const N : usize > FillError < T , N > {
217- fn new ( ) -> Self {
216+ /// Creates a new empty `FillError` which can be used
217+ /// to build `[T; N]` from an iterator.
218+ pub fn new ( ) -> Self {
218219 Self { array : MaybeUninit :: uninit_array ( ) , len : 0 }
219220 }
220221
@@ -242,8 +243,8 @@ impl<T, const N: usize> FillError<T, N> {
242243 for i in self . len ..N {
243244 if let Some ( value) = iter. next ( ) {
244245 self . array [ i] . write ( value) ;
246+ self . len = i + 1 ;
245247 } else {
246- self . len = i;
247248 return Err ( self ) ;
248249 }
249250 }
@@ -265,14 +266,6 @@ impl<T, const N: usize> FillError<T, N> {
265266 }
266267}
267268
268- #[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
269- impl < T , const N : usize > FromIterator < T > for Result < [ T ; N ] , FillError < T , N > > {
270- #[ inline]
271- fn from_iter < I : IntoIterator < Item = T > > ( iter : I ) -> Self {
272- FillError :: < T , N > :: new ( ) . fill ( iter)
273- }
274- }
275-
276269#[ stable( feature = "rust1" , since = "1.0.0" ) ]
277270impl < ' a , T , const N : usize > IntoIterator for & ' a [ T ; N ] {
278271 type Item = & ' a T ;
@@ -484,42 +477,11 @@ impl<T, const N: usize> [T; N] {
484477 /// assert_eq!(y, [6, 9, 3, 3]);
485478 /// ```
486479 #[ unstable( feature = "array_map" , issue = "75243" ) ]
487- pub fn map < F , U > ( self , mut f : F ) -> [ U ; N ]
480+ pub fn map < F , U > ( self , f : F ) -> [ U ; N ]
488481 where
489482 F : FnMut ( T ) -> U ,
490483 {
491- use crate :: mem:: MaybeUninit ;
492- struct Guard < T , const N : usize > {
493- dst : * mut T ,
494- initialized : usize ,
495- }
496-
497- impl < T , const N : usize > Drop for Guard < T , N > {
498- fn drop ( & mut self ) {
499- debug_assert ! ( self . initialized <= N ) ;
500-
501- let initialized_part =
502- crate :: ptr:: slice_from_raw_parts_mut ( self . dst , self . initialized ) ;
503- // SAFETY: this raw slice will contain only initialized objects
504- // that's why, it is allowed to drop it.
505- unsafe {
506- crate :: ptr:: drop_in_place ( initialized_part) ;
507- }
508- }
509- }
510- let mut dst = MaybeUninit :: uninit_array :: < N > ( ) ;
511- let mut guard: Guard < U , N > =
512- Guard { dst : MaybeUninit :: slice_as_mut_ptr ( & mut dst) , initialized : 0 } ;
513- for ( src, dst) in IntoIter :: new ( self ) . zip ( & mut dst) {
514- dst. write ( f ( src) ) ;
515- guard. initialized += 1 ;
516- }
517- // FIXME: Convert to crate::mem::transmute once it works with generics.
518- // unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) }
519- crate :: mem:: forget ( guard) ;
520- // SAFETY: At this point we've properly initialized the whole array
521- // and we just need to cast it to the correct type.
522- unsafe { crate :: mem:: transmute_copy :: < _ , [ U ; N ] > ( & dst) }
484+ FillError :: new ( ) . fill ( IntoIter :: new ( self ) . map ( f) ) . unwrap ( )
523485 }
524486
525487 /// Returns a slice containing the entire array. Equivalent to `&s[..]`.
0 commit comments