Skip to content

Commit 2cba121

Browse files
committed
Add recommend changes to array
Switch from indexing to zip, and also use `write` on `MaybeUninit`. Add array_map feature to core/src/lib Attempt to fix issue of no such feature Update w/ pickfire's review This changes a couple of names around, adds another small test of variable size, and hides the rustdoc #![feature(..)]. Fmt doctest Add suggestions from lcnr
1 parent 0bb603d commit 2cba121

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

library/core/src/array/mod.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,28 @@ impl<T, const N: usize> [T; N] {
372372
///
373373
/// # Examples
374374
/// ```
375-
/// let x = [1,2,3];
375+
/// # #![feature(array_map)]
376+
/// let x = [1, 2, 3];
376377
/// let y = x.map(|v| v + 1);
377-
/// assert_eq!(y, [2,3,4]);
378+
/// assert_eq!(y, [2, 3, 4]);
378379
/// ```
379380
#[unstable(feature = "array_map", issue = "77777")]
380-
pub fn map<F, S>(self, mut f: F) -> [S; N]
381+
pub fn map<F, U>(self, mut f: F) -> [U; N]
381382
where
382-
F: FnMut(T) -> S,
383+
F: FnMut(T) -> U,
383384
{
384385
use crate::mem::MaybeUninit;
385386
struct Guard<T, const N: usize> {
386387
dst: *mut T,
387-
curr_init: usize,
388+
initialized: usize,
388389
}
389390

390391
impl<T, const N: usize> Drop for Guard<T, N> {
391392
fn drop(&mut self) {
392-
debug_assert!(self.curr_init <= N);
393+
debug_assert!(self.initialized <= N);
393394

394395
let initialized_part =
395-
crate::ptr::slice_from_raw_parts_mut(self.dst, self.curr_init);
396+
crate::ptr::slice_from_raw_parts_mut(self.dst, self.initialized);
396397
// SAFETY: this raw slice will contain only initialized objects
397398
// that's why, it is allowed to drop it.
398399
unsafe {
@@ -401,16 +402,16 @@ impl<T, const N: usize> [T; N] {
401402
}
402403
}
403404
let mut dst = MaybeUninit::uninit_array::<N>();
404-
let mut guard: Guard<S, N> = Guard { dst: &mut dst as *mut _ as *mut S, curr_init: 0 };
405-
for (i, e) in IntoIter::new(self).enumerate() {
406-
dst[i] = MaybeUninit::new(f(e));
407-
guard.curr_init += 1;
405+
let mut guard: Guard<U, N> = Guard { dst: &mut dst as *mut _ as *mut U, initialized: 0 };
406+
for (src, dst) in IntoIter::new(self).zip(&mut dst) {
407+
dst.write(f(src));
408+
guard.initialized += 1;
408409
}
409-
// FIXME convert to crate::mem::transmute when works with generics
410-
// unsafe { crate::mem::transmute::<[MaybeUninit<S>; N], [S; N]>(dst) }
410+
// FIXME: Convert to crate::mem::transmute once it works with generics.
411+
// unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) }
411412
crate::mem::forget(guard);
412413
// SAFETY: At this point we've properly initialized the whole array
413-
// and we just need to cast it to the correct type
414-
unsafe { (&mut dst as *mut _ as *mut [S; N]).read() }
414+
// and we just need to cast it to the correct type.
415+
unsafe { (&mut dst as *mut _ as *mut [U; N]).read() }
415416
}
416417
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
#![feature(abi_unadjusted)]
146146
#![feature(adx_target_feature)]
147147
#![feature(maybe_uninit_slice)]
148+
#![feature(maybe_uninit_extra)]
148149
#![feature(external_doc)]
149150
#![feature(associated_type_bounds)]
150151
#![feature(const_caller_location)]

library/core/tests/array.rs

+4
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,8 @@ fn array_map() {
296296
let a = [1, 2, 3];
297297
let b = a.map(|v| v + 1);
298298
assert_eq!(b, [2, 3, 4]);
299+
300+
let a = [1u8, 2, 3];
301+
let b = a.map(|v| v as u64);
302+
assert_eq!(b, [1, 2, 3]);
299303
}

0 commit comments

Comments
 (0)