]> git.lizzy.rs Git - rust.git/commitdiff
Use `drop_in_place` in `array::IntoIter::drop`
authorSimon Sapin <simon.sapin@exyr.org>
Fri, 25 Oct 2019 16:11:20 +0000 (18:11 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Mon, 18 Nov 2019 14:56:26 +0000 (15:56 +0100)
This skips the loop when the element type is known not to have drop glue, even in debug mode.

src/libcore/array/iter.rs

index 307e9b90ee2c0cfe1f62e6f875867796453534ea..aab9463e3aade248452fb74a883dd77e92ab28a4 100644 (file)
@@ -92,6 +92,18 @@ fn as_slice(&self) -> &[T] {
             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 @@ impl<T, const N: usize> Drop for IntoIter<T, {N}>
     [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())
+        }
     }
 }