document why we're not directly passing drop_ptr to drop_in_place

This commit is contained in:
The8472 2021-11-20 01:29:04 +01:00
parent ce994027fe
commit 6851b8d931

View File

@ -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;