-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
use ManuallyDrop instead of forget inside collections #70766
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
use core::alloc::MemoryBlock; | ||
use core::cmp; | ||
use core::mem::{self, MaybeUninit}; | ||
use core::mem::{self, ManuallyDrop, MaybeUninit}; | ||
use core::ops::Drop; | ||
use core::ptr::{NonNull, Unique}; | ||
use core::slice; | ||
|
@@ -112,11 +112,10 @@ impl<T> RawVec<T, Global> { | |
} | ||
|
||
/// Converts a `Box<[T]>` into a `RawVec<T>`. | ||
pub fn from_box(mut slice: Box<[T]>) -> Self { | ||
pub fn from_box(slice: Box<[T]>) -> Self { | ||
unsafe { | ||
let result = RawVec::from_raw_parts(slice.as_mut_ptr(), slice.len()); | ||
mem::forget(slice); | ||
result | ||
let mut slice = ManuallyDrop::new(slice); | ||
RawVec::from_raw_parts(slice.as_mut_ptr(), slice.len()) | ||
} | ||
} | ||
} | ||
|
@@ -579,11 +578,10 @@ impl<T> RawVec<T, Global> { | |
"`len` must be smaller than or equal to `self.capacity()`" | ||
); | ||
|
||
let me = ManuallyDrop::new(self); | ||
// NOTE: not calling `capacity()` here; actually using the real `cap` field! | ||
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. Any idea what is up with that comment? 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. 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. Looks like just an outdated comment then. Do you want to remove it? 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. This PR is already r+-and-rollup-ed, so I'll remove the comment once the PR is merged to avoid merge issues. 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. Ah I'll just add it to #70776 then 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. Yes, the comment is outdated and I forgot to remove it. |
||
let slice = slice::from_raw_parts_mut(self.ptr() as *mut MaybeUninit<T>, len); | ||
let output = Box::from_raw(slice); | ||
mem::forget(self); | ||
output | ||
let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len); | ||
Box::from_raw(slice) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cc @Mark-Simulacrum @ssomers for BTreeMap changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hardly understand ManuallyDrop, but isn't it a good fit for btree::node::Root? I don't really understand Root's comment "Note that this does not have a destructor," to begin with. Doesn't Rust generate a destructor if you don't implement Drop? So the comment should say: "Note that we (hopefully) do not invoke the generated destructor, because we need to clean up the entire tree and we do that in a fancy and dangerous way while iterating".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose that comment just means that there is no
Drop
impl that would clean up the data behind the raw pointers inRoot
-- usually one would expect such animpl
to exist.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay. But is that something more formally expressed by making Root.node ManuallyDrop? I should just try it and learn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, putting a raw pointer (or Unique) behind manually drop is pointless.