about summary refs log tree commit diff
path: root/src/libcore/array
diff options
context:
space:
mode:
authorPeter <peter.wilkins@polecat.com>2019-12-08 23:16:18 +0000
committerPeter <peter.wilkins@polecat.com>2019-12-08 23:49:30 +0000
commit8f6a06285efe12d778ff7f44067aebeed7b14428 (patch)
tree856b9d17a8e4700c44a2794e63ec75ecf9660397 /src/libcore/array
parent947772fc31b96ce90f57720f74571f14e35df66b (diff)
parent59947fcae6a40df12e33af8c8c7291014b7603e0 (diff)
downloadrust-8f6a06285efe12d778ff7f44067aebeed7b14428.tar.gz
rust-8f6a06285efe12d778ff7f44067aebeed7b14428.zip
move from non zero impls to `libcore/convert/num.rs`
Diffstat (limited to 'src/libcore/array')
-rw-r--r--src/libcore/array/iter.rs22
-rw-r--r--src/libcore/array/mod.rs4
2 files changed, 19 insertions, 7 deletions
diff --git a/src/libcore/array/iter.rs b/src/libcore/array/iter.rs
index 307e9b90ee2..aab9463e3aa 100644
--- a/src/libcore/array/iter.rs
+++ b/src/libcore/array/iter.rs
@@ -92,6 +92,18 @@ where
             mem::transmute::<&[MaybeUninit<T>], &[T]>(slice)
         }
     }
+
+    /// Returns a mutable slice of all elements that have not been yielded yet.
+    fn as_mut_slice(&mut self) -> &mut [T] {
+        // This transmute is safe, same as in `as_slice` above.
+        let slice = &mut self.data[self.alive.clone()];
+        // SAFETY: This transmute is safe. As mentioned in `new`, `MaybeUninit` retains
+        // the size and alignment of `T`. Furthermore, we know that all
+        // elements within `alive` are properly initialized.
+        unsafe {
+            mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(slice)
+        }
+    }
 }
 
 
@@ -184,10 +196,12 @@ where
     [T; N]: LengthAtMost32,
 {
     fn drop(&mut self) {
-        // We simply drop each element via `for_each`. This should not incur
-        // any significant runtime overhead and avoids adding another `unsafe`
-        // block.
-        self.by_ref().for_each(drop);
+        // SAFETY: This is safe: `as_mut_slice` returns exactly the sub-slice
+        // of elements that have not been moved out yet and that remain
+        // to be dropped.
+        unsafe {
+            ptr::drop_in_place(self.as_mut_slice())
+        }
     }
 }
 
diff --git a/src/libcore/array/mod.rs b/src/libcore/array/mod.rs
index 74a7d062d3f..38d248d701d 100644
--- a/src/libcore/array/mod.rs
+++ b/src/libcore/array/mod.rs
@@ -1,5 +1,5 @@
 //! Implementations of things like `Eq` for fixed-length arrays
-//! up to a certain length. Eventually we should able to generalize
+//! up to a certain length. Eventually, we should be able to generalize
 //! to all lengths.
 //!
 //! *[See also the array primitive type](../../std/primitive.array.html).*
@@ -14,10 +14,8 @@ use crate::hash::{Hash, self};
 use crate::marker::Unsize;
 use crate::slice::{Iter, IterMut};
 
-#[cfg(not(bootstrap))]
 mod iter;
 
-#[cfg(not(bootstrap))]
 #[unstable(feature = "array_value_iter", issue = "65798")]
 pub use iter::IntoIter;