Skip to content

Commit

Permalink
Rollup merge of rust-lang#65821 - SimonSapin:in-place, r=Amanieu
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohnTitor authored Nov 13, 2019
2 parents 374ad1b + f4c59b3 commit c309266
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/libcore/array/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ 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()];
unsafe {
mem::transmute::<&mut [MaybeUninit<T>], &mut [T]>(slice)
}
}
}


Expand Down Expand Up @@ -184,10 +193,9 @@ 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);
unsafe {
ptr::drop_in_place(self.as_mut_slice())
}
}
}

Expand Down

0 comments on commit c309266

Please sign in to comment.