#[cfg(not(bootstrap))]
#[lang = "array"]
impl<T, const N: usize> [T; N] {
- /// Returns an array of the same size as self, with `f` applied to each element.
+ /// Returns an array of the same size as `self`, with function `f` applied to each element
+ /// in order.
///
/// # Examples
+ ///
/// ```
- /// let x = [1,2,3];
+ /// #![feature(array_map)]
+ /// let x = [1, 2, 3];
/// let y = x.map(|v| v + 1);
- /// assert_eq!(y, [2,3,4]);
+ /// assert_eq!(y, [2, 3, 4]);
+ ///
+ /// let x = [1, 2, 3];
+ /// let mut temp = 0;
+ /// let y = x.map(|v| { temp += 1; v * temp });
+ /// assert_eq!(y, [1, 4, 9]);
+ ///
+ /// let x = ["Ferris", "Bueller's", "Day", "Off"];
+ /// let y = x.map(|v| v.len());
+ /// assert_eq!(y, [6, 9, 3, 3]);
/// ```
- #[unstable(feature = "array_map", issue = "77777")]
- pub fn map<F, S>(self, mut f: F) -> [S; N]
+ #[unstable(feature = "array_map", issue = "75243")]
+ pub fn map<F, U>(self, mut f: F) -> [U; N]
where
- F: FnMut(T) -> S,
+ F: FnMut(T) -> U,
{
use crate::mem::MaybeUninit;
struct Guard<T, const N: usize> {
dst: *mut T,
- curr_init: usize,
+ initialized: usize,
}
impl<T, const N: usize> Drop for Guard<T, N> {
fn drop(&mut self) {
- debug_assert!(self.curr_init <= N);
+ debug_assert!(self.initialized <= N);
let initialized_part =
- crate::ptr::slice_from_raw_parts_mut(self.dst, self.curr_init);
+ crate::ptr::slice_from_raw_parts_mut(self.dst, self.initialized);
// SAFETY: this raw slice will contain only initialized objects
// that's why, it is allowed to drop it.
unsafe {
}
}
let mut dst = MaybeUninit::uninit_array::<N>();
- let mut guard: Guard<S, N> = Guard { dst: &mut dst as *mut _ as *mut S, curr_init: 0 };
- for (i, e) in IntoIter::new(self).enumerate() {
- dst[i] = MaybeUninit::new(f(e));
- guard.curr_init += 1;
+ let mut guard: Guard<U, N> =
+ Guard { dst: MaybeUninit::first_ptr_mut(&mut dst), initialized: 0 };
+ for (src, dst) in IntoIter::new(self).zip(&mut dst) {
+ dst.write(f(src));
+ guard.initialized += 1;
}
- // FIXME convert to crate::mem::transmute when works with generics
- // unsafe { crate::mem::transmute::<[MaybeUninit<S>; N], [S; N]>(dst) }
+ // FIXME: Convert to crate::mem::transmute once it works with generics.
+ // unsafe { crate::mem::transmute::<[MaybeUninit<U>; N], [U; N]>(dst) }
crate::mem::forget(guard);
// SAFETY: At this point we've properly initialized the whole array
- // and we just need to cast it to the correct type
- unsafe { (&mut dst as *mut _ as *mut [S; N]).read() }
+ // and we just need to cast it to the correct type.
+ unsafe { crate::mem::transmute_copy::<_, [U; N]>(&dst) }
}
}