-
Notifications
You must be signed in to change notification settings - Fork 184
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
Replace mem::forget
with ManuallyDrop::new
#3515
Conversation
It's way less footgunny w.r.t. pointer aliasing: - At worst, it's equivalent; - But the advantage of `ManuallyDrop::new()`, is that it can be used _before_ extracting data off the value, such as pointers, thereby avoiding the danger of aliasing invalidation that `mem::forget` may apply.
@danielhenrymantilla-codespace unfortunately you'll need to sign the CLA for us to be able to land this. Otherwise I can try and duplicate your PR: I haven't yet looked at the contents here. |
This comment was marked as outdated.
This comment was marked as outdated.
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.
r=me as well, even though I can't approve my own PR
let capacity = vec.capacity(); | ||
mem::forget(vec); | ||
let len = vec.len(); | ||
let ptr = mem::ManuallyDrop::new(vec).as_mut_ptr(); |
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.
ah, this is interesting, I was trying to figure out how best to fix the problem here
Thanks so much, this is great! |
Part of #3510
Extracted from @danielhenrymantilla's PR #3514
It's way less footgunny w.r.t. pointer aliasing:
ManuallyDrop::new()
, is that it can be usedbefore extracting data off the value, such as pointers, thereby
avoiding the danger of aliasing invalidation that
mem::forget
mayapply.