Use `ManuallyDrop<T>` instead of `mem::forget`

`mem::forget` should usually not be used in unsafe code, as it's
a little footgunny. `mem::forget` does a typed move _after_ the type
has been moved out of, which is bad, as typed moves assert validity,
but types can become invalid once they have been moved out of.
In this specific example it's not a soundness problem, but we should
promote the better style of using `ManuallyDrop<T>` instead.
pull/381/head
Nilstrieb 3 years ago
parent 6956c60286
commit eb351ec767
No known key found for this signature in database

@ -60,13 +60,13 @@ impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
// Can't destructure Vec since it's Drop
let ptr = self.ptr;
let cap = self.cap;
let len = self.len;
// Make sure not to drop Vec since that would free the buffer
mem::forget(self);
let vec = ManuallyDrop::new(self);
// Can't destructure Vec since it's Drop
let ptr = vec.ptr;
let cap = vec.cap;
let len = vec.len;
unsafe {
IntoIter {

Loading…
Cancel
Save