Skip to content

Commit 94ec272

Browse files
committed
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
1 parent 946233c commit 94ec272

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

library/core/src/array/mod.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -372,28 +372,28 @@ impl<T, const N: usize> [T; N] {
372372
///
373373
/// # Examples
374374
/// ```
375-
/// #![feature(array_map)]
376-
/// let x = [1,2,3];
375+
/// # #![feature(array_map)]
376+
/// let x = [1, 2, 3];
377377
/// let y = x.map(|v| v + 1);
378-
/// assert_eq!(y, [2,3,4]);
378+
/// assert_eq!(y, [2, 3, 4]);
379379
/// ```
380380
#[unstable(feature = "array_map", issue = "77777")]
381-
pub fn map<F, S>(self, mut f: F) -> [S; N]
381+
pub fn map<F, U>(self, mut f: F) -> [U; N]
382382
where
383-
F: FnMut(T) -> S,
383+
F: FnMut(T) -> U,
384384
{
385385
use crate::mem::MaybeUninit;
386386
struct Guard<T, const N: usize> {
387387
dst: *mut T,
388-
curr_init: usize,
388+
initialized: usize,
389389
}
390390

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

395395
let initialized_part =
396-
crate::ptr::slice_from_raw_parts_mut(self.dst, self.curr_init);
396+
crate::ptr::slice_from_raw_parts_mut(self.dst, self.initialized);
397397
// SAFETY: this raw slice will contain only initialized objects
398398
// that's why, it is allowed to drop it.
399399
unsafe {
@@ -402,16 +402,16 @@ impl<T, const N: usize> [T; N] {
402402
}
403403
}
404404
let mut dst = MaybeUninit::uninit_array::<N>();
405-
let mut guard: Guard<S, N> = Guard { dst: &mut dst as *mut _ as *mut S, curr_init: 0 };
405+
let mut guard: Guard<U, N> = Guard { dst: &mut dst as *mut _ as *mut U, initialized: 0 };
406406
for (src, dst) in IntoIter::new(self).zip(&mut dst) {
407407
dst.write(f(src));
408-
guard.curr_init += 1;
408+
guard.initialized += 1;
409409
}
410410
// FIXME convert to crate::mem::transmute when works with generics
411-
// unsafe { crate::mem::transmute::<[MaybeUninit<S>; N], [S; N]>(dst) }
411+
// unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) }
412412
crate::mem::forget(guard);
413413
// SAFETY: At this point we've properly initialized the whole array
414414
// and we just need to cast it to the correct type
415-
unsafe { (&mut dst as *mut _ as *mut [S; N]).read() }
415+
unsafe { (&mut dst as *mut _ as *mut [U; N]).read() }
416416
}
417417
}

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)