Skip to content

Commit 5c5a120

Browse files
committed
Auto merge of #65821 - SimonSapin:in-place, r=Amanieu
Use `drop_in_place` in `array::IntoIter::drop` This skips the loop when the element type is known not to have drop glue, even in debug mode.
2 parents 0ccee30 + 74b5714 commit 5c5a120

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/libcore/array/iter.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ where
9292
mem::transmute::<&[MaybeUninit<T>], &[T]>(slice)
9393
}
9494
}
95+
96+
/// Returns a mutable slice of all elements that have not been yielded yet.
97+
fn as_mut_slice(&mut self) -> &mut [T] {
98+
// This transmute is safe, same as in `as_slice` above.
99+
let slice = &mut self.data[self.alive.clone()];
100+
// SAFETY: This transmute is safe. As mentioned in `new`, `MaybeUninit` retains
101+
// the size and alignment of `T`. Furthermore, we know that all
102+
// elements within `alive` are properly initialized.
103+
unsafe {
104+
mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(slice)
105+
}
106+
}
95107
}
96108

97109

@@ -184,10 +196,12 @@ where
184196
[T; N]: LengthAtMost32,
185197
{
186198
fn drop(&mut self) {
187-
// We simply drop each element via `for_each`. This should not incur
188-
// any significant runtime overhead and avoids adding another `unsafe`
189-
// block.
190-
self.by_ref().for_each(drop);
199+
// SAFETY: This is safe: `as_mut_slice` returns exactly the sub-slice
200+
// of elements that have not been moved out yet and that remain
201+
// to be dropped.
202+
unsafe {
203+
ptr::drop_in_place(self.as_mut_slice())
204+
}
191205
}
192206
}
193207

0 commit comments

Comments
 (0)