about summary refs log tree commit diff
path: root/library/core/src/array
diff options
context:
space:
mode:
authorkadmin <julianknodt@gmail.com>2020-08-06 23:36:50 +0000
committerkadmin <julianknodt@gmail.com>2020-08-13 03:50:59 +0000
commit56a651ca15ac2d3f74259b8df8ac07bd80dbeaf8 (patch)
tree853ae9fbc115c61fd4af7786e9041edd469dedbe /library/core/src/array
parentf6411e4c666248d7a74413da03f31b761cf8f66f (diff)
downloadrust-56a651ca15ac2d3f74259b8df8ac07bd80dbeaf8.tar.gz
rust-56a651ca15ac2d3f74259b8df8ac07bd80dbeaf8.zip
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
Diffstat (limited to 'library/core/src/array')
-rw-r--r--library/core/src/array/mod.rs31
1 files changed, 16 insertions, 15 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs
index f85f5efbb9a..16baef137cb 100644
--- a/library/core/src/array/mod.rs
+++ b/library/core/src/array/mod.rs
@@ -372,27 +372,28 @@ impl<T, const N: usize> [T; N] {
     ///
     /// # 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]);
     /// ```
     #[unstable(feature = "array_map", issue = "77777")]
-    pub fn map<F, S>(self, mut f: F) -> [S; N]
+    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 {
@@ -401,16 +402,16 @@ impl<T, const N: usize> [T; N] {
             }
         }
         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: &mut dst as *mut _ as *mut U, 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 { (&mut dst as *mut _ as *mut [U; N]).read() }
     }
 }