Skip to content

Commit 6851b8d

Browse files
committed
document why we're not directly passing drop_ptr to drop_in_place
1 parent ce99402 commit 6851b8d

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

library/alloc/src/vec/drain.rs

+4
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
156156
}
157157

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

0 commit comments

Comments
 (0)