@@ -11,7 +11,6 @@ use crate::cmp::Ordering;
11
11
use crate :: convert:: { Infallible , TryFrom } ;
12
12
use crate :: fmt;
13
13
use crate :: hash:: { self , Hash } ;
14
- use crate :: iter:: FromIterator ;
15
14
use crate :: marker:: Unsize ;
16
15
use crate :: mem:: MaybeUninit ;
17
16
use crate :: slice:: { Iter , IterMut } ;
@@ -176,7 +175,10 @@ impl<T: fmt::Debug, const N: usize> fmt::Debug for [T; N] {
176
175
}
177
176
}
178
177
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.
180
182
#[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
181
183
pub struct FillError < T , const N : usize > {
182
184
array : [ MaybeUninit < T > ; N ] ,
@@ -194,27 +196,26 @@ impl<T, const N: usize> fmt::Display for FillError<T, N> {
194
196
}
195
197
196
198
#[ 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 > {
198
200
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)
203
202
}
204
203
}
205
204
206
205
#[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
207
206
impl < T , const N : usize > Drop for FillError < T , N > {
208
207
fn drop ( & mut self ) {
209
208
// 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.
211
210
unsafe { crate :: ptr:: drop_in_place ( self . as_mut_slice ( ) ) }
212
211
}
213
212
}
214
213
215
214
#[ unstable( feature = "array_from_iter_impl" , issue = "none" ) ]
216
215
impl < 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 {
218
219
Self { array : MaybeUninit :: uninit_array ( ) , len : 0 }
219
220
}
220
221
@@ -242,8 +243,8 @@ impl<T, const N: usize> FillError<T, N> {
242
243
for i in self . len ..N {
243
244
if let Some ( value) = iter. next ( ) {
244
245
self . array [ i] . write ( value) ;
246
+ self . len = i + 1 ;
245
247
} else {
246
- self . len = i;
247
248
return Err ( self ) ;
248
249
}
249
250
}
@@ -265,14 +266,6 @@ impl<T, const N: usize> FillError<T, N> {
265
266
}
266
267
}
267
268
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
-
276
269
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
277
270
impl < ' a , T , const N : usize > IntoIterator for & ' a [ T ; N ] {
278
271
type Item = & ' a T ;
@@ -484,42 +477,11 @@ impl<T, const N: usize> [T; N] {
484
477
/// assert_eq!(y, [6, 9, 3, 3]);
485
478
/// ```
486
479
#[ 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 ]
488
481
where
489
482
F : FnMut ( T ) -> U ,
490
483
{
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 ( )
523
485
}
524
486
525
487
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
0 commit comments