Skip to content

Commit

Permalink
document why we're not directly passing drop_ptr to drop_in_place
Browse files Browse the repository at this point in the history
  • Loading branch information
the8472 committed Nov 20, 2021
1 parent ce99402 commit 6851b8d
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/alloc/src/vec/drain.rs
Expand Up @@ -156,6 +156,10 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
}

unsafe {
// drop_ptr comes from a slice::Iter which only gives us a &[T] but for drop_in_place
// a pointer with mutable provenance is necessary. Therefore we must reconstruct
// it from the original vec but also avoid creating a &mut to the front since that could
// invalidate raw pointers to it which some unsafe code might rely on.
let vec = vec.as_mut();
let spare_capacity = vec.spare_capacity_mut();
let drop_offset = drop_ptr.offset_from(spare_capacity.as_ptr() as *const _) as usize;
Expand Down

0 comments on commit 6851b8d

Please sign in to comment.