-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Vec drop and truncate: drop using raw slice *mut [T] #71148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -741,7 +741,7 @@ impl<T> Vec<T> { | |
return; | ||
} | ||
let remaining_len = self.len - len; | ||
let s = slice::from_raw_parts_mut(self.as_mut_ptr().add(len), remaining_len); | ||
let s = ptr::slice_from_raw_parts_mut(self.as_mut_ptr().add(len), remaining_len); | ||
self.len = len; | ||
ptr::drop_in_place(s); | ||
} | ||
|
@@ -2379,7 +2379,9 @@ unsafe impl<#[may_dangle] T> Drop for Vec<T> { | |
fn drop(&mut self) { | ||
unsafe { | ||
// use drop for [T] | ||
ptr::drop_in_place(&mut self[..]); | ||
// use a raw slice to refer to the elements of the vector as weakest necessary type; | ||
// could avoid questions of validity in certain cases | ||
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(self.as_mut_ptr(), self.len)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth adding a brief comment for why we use this complicated operation here instead of the simple one. Really, already for #70558, what we should really have is a way to get a raw slice for the entire There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment added, though it's very vague since I don't dare say so much about what it actually allows |
||
} | ||
// RawVec handles deallocation | ||
} | ||
|
@@ -2596,7 +2598,11 @@ impl<T> IntoIter<T> { | |
/// ``` | ||
#[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")] | ||
pub fn as_mut_slice(&mut self) -> &mut [T] { | ||
unsafe { slice::from_raw_parts_mut(self.ptr as *mut T, self.len()) } | ||
unsafe { &mut *self.as_raw_mut_slice() } | ||
} | ||
|
||
fn as_raw_mut_slice(&mut self) -> *mut [T] { | ||
ptr::slice_from_raw_parts_mut(self.ptr as *mut T, self.len()) | ||
} | ||
} | ||
|
||
|
@@ -2708,7 +2714,7 @@ unsafe impl<#[may_dangle] T> Drop for IntoIter<T> { | |
let guard = DropGuard(self); | ||
// destroy the remaining elements | ||
unsafe { | ||
ptr::drop_in_place(guard.0.as_mut_slice()); | ||
ptr::drop_in_place(guard.0.as_raw_mut_slice()); | ||
} | ||
// now `guard` will be dropped and do the rest | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.