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 22:08:56 +0000
committerkadmin <julianknodt@gmail.com>2020-08-13 03:50:57 +0000
commitf6411e4c666248d7a74413da03f31b761cf8f66f (patch)
tree6c58d67f14b8f7b24aac3b44659a519de2d5aaba /library/core/src/array
parentd8718183b2f3e0dacc2731b5c84fe7b0eb197a6e (diff)
downloadrust-f6411e4c666248d7a74413da03f31b761cf8f66f.tar.gz
rust-f6411e4c666248d7a74413da03f31b761cf8f66f.zip
Add Array Impl Lang Item in various places
Add basic test

And also run fmt which is where the other changes are from

Fix mut issues

These only appear when running tests, so resolved by adding mut

Swap order of forget

Add pub and rm guard impl

Add explicit type to guard

Add safety note

Change guard type from T to S

It should never have been T, as it guards over [MaybeUninit<S>; N]
Also add feature to test
Diffstat (limited to 'library/core/src/array')
-rw-r--r--library/core/src/array/mod.rs17
1 files changed, 7 insertions, 10 deletions
diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs
index b549cd8959f..f85f5efbb9a 100644
--- a/library/core/src/array/mod.rs
+++ b/library/core/src/array/mod.rs
@@ -377,7 +377,7 @@ impl<T, const N: usize> [T; N] {
     /// assert_eq!(y, [2,3,4]);
     /// ```
     #[unstable(feature = "array_map", issue = "77777")]
-    fn map<F, S>(self, f: F) -> [S; N]
+    pub fn map<F, S>(self, mut f: F) -> [S; N]
     where
         F: FnMut(T) -> S,
     {
@@ -387,12 +387,6 @@ impl<T, const N: usize> [T; N] {
             curr_init: usize,
         }
 
-        impl<T, const N: usize> Guard<T, N> {
-            fn new(dst: &mut [MaybeUninit<T>; N]) -> Self {
-                Guard { dst: dst as *mut _ as *mut T, curr_init: 0 }
-            }
-        }
-
         impl<T, const N: usize> Drop for Guard<T, N> {
             fn drop(&mut self) {
                 debug_assert!(self.curr_init <= N);
@@ -406,14 +400,17 @@ impl<T, const N: usize> [T; N] {
                 }
             }
         }
-        let dst = MaybeUninit::uninit_array::<N>();
-        let mut guard = Guard::new(&mut dst);
-        for (i, e) in self.into_iter().enumerate() {
+        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;
         }
         // FIXME convert to crate::mem::transmute when works with generics
         // unsafe { crate::mem::transmute::<[MaybeUninit<S>; N], [S; 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() }
     }
 }