Skip to content

Commit f654daf

Browse files
committed
Vec IntoIter: Drop using raw slice
Update Vec drop with a comment to explain why we want to use a raw slice, and extend this pattern to also include the Vec's IntoIter.
1 parent 7612ad7 commit f654daf

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/liballoc/vec.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2379,6 +2379,8 @@ unsafe impl<#[may_dangle] T> Drop for Vec<T> {
23792379
fn drop(&mut self) {
23802380
unsafe {
23812381
// use drop for [T]
2382+
// use a raw slice to refer to the elements of the vector as weakest necessary type;
2383+
// could avoid questions of validity in certain cases
23822384
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len))
23832385
}
23842386
// RawVec handles deallocation
@@ -2596,7 +2598,11 @@ impl<T> IntoIter<T> {
25962598
/// ```
25972599
#[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
25982600
pub fn as_mut_slice(&mut self) -> &mut [T] {
2599-
unsafe { slice::from_raw_parts_mut(self.ptr as *mut T, self.len()) }
2601+
unsafe { &mut *self.as_raw_mut_slice() }
2602+
}
2603+
2604+
fn as_raw_mut_slice(&mut self) -> *mut [T] {
2605+
ptr::slice_from_raw_parts_mut(self.ptr as *mut T, self.len())
26002606
}
26012607
}
26022608

@@ -2708,7 +2714,7 @@ unsafe impl<#[may_dangle] T> Drop for IntoIter<T> {
27082714
let guard = DropGuard(self);
27092715
// destroy the remaining elements
27102716
unsafe {
2711-
ptr::drop_in_place(guard.0.as_mut_slice());
2717+
ptr::drop_in_place(guard.0.as_raw_mut_slice());
27122718
}
27132719
// now `guard` will be dropped and do the rest
27142720
}

0 commit comments

Comments
 (0)