]> git.lizzy.rs Git - rust.git/blobdiff - library/core/src/array/mod.rs
Rollup merge of #75485 - RalfJung:pin, r=nagisa
[rust.git] / library / core / src / array / mod.rs
index 01bf9d5ef1e80a656a4cd6ef77599b843a1d9e03..6b28ab7d755631f68860ed2cd402f9d1cb20c418 100644 (file)
@@ -368,13 +368,13 @@ macro_rules! array_impl_default {
 #[cfg(not(bootstrap))]
 #[lang = "array"]
 impl<T, const N: usize> [T; N] {
-    /// Returns an array of the same size as `self`, with function `f` applied to each element.
-    /// The closure will be called on elements 0 up to but excluding N.
+    /// Returns an array of the same size as `self`, with function `f` applied to each element
+    /// in order.
     ///
     /// # Examples
     ///
     /// ```
-    /// # #![feature(array_map)]
+    /// #![feature(array_map)]
     /// let x = [1, 2, 3];
     /// let y = x.map(|v| v + 1);
     /// assert_eq!(y, [2, 3, 4]);
@@ -383,6 +383,10 @@ macro_rules! array_impl_default {
     /// 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 = "75243")]
     pub fn map<F, U>(self, mut f: F) -> [U; N]
@@ -409,7 +413,8 @@ fn drop(&mut self) {
             }
         }
         let mut dst = MaybeUninit::uninit_array::<N>();
-        let mut guard: Guard<U, N> = Guard { dst: &mut dst as *mut _ as *mut U, initialized: 0 };
+        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;
@@ -419,6 +424,6 @@ fn drop(&mut self) {
         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 [U; N]).read() }
+        unsafe { crate::mem::transmute_copy::<_, [U; N]>(&dst) }
     }
 }