Skip to content
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

Merged
merged 3 commits into from
Jun 8, 2023

Conversation

Manishearth
Copy link
Member

@Manishearth Manishearth commented Jun 8, 2023

Part of #3510

Extracted from @danielhenrymantilla's PR #3514

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.

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.
@Manishearth Manishearth requested a review from sffc as a code owner June 8, 2023 14:07
@Manishearth Manishearth requested a review from robertbastian June 8, 2023 14:07
@CLAassistant
Copy link

CLAassistant commented Jun 8, 2023

CLA assistant check
All committers have signed the CLA.

@Manishearth
Copy link
Member Author

@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.

@danielhenrymantilla

This comment was marked as outdated.

robertbastian
robertbastian previously approved these changes Jun 8, 2023
utils/zerovec/src/yoke_impls.rs Show resolved Hide resolved
utils/zerovec/src/zerovec/mod.rs Outdated Show resolved Hide resolved
robertbastian
robertbastian previously approved these changes Jun 8, 2023
Copy link
Member Author

@Manishearth Manishearth left a 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();
Copy link
Member Author

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

@Manishearth
Copy link
Member Author

Thanks so much, this is great!

@Manishearth Manishearth merged commit c010e99 into unicode-org:main Jun 8, 2023
@Manishearth Manishearth deleted the zv-forget branch June 8, 2023 16:05
@sffc sffc removed their request for review June 8, 2023 16:14
@Manishearth Manishearth mentioned this pull request Sep 21, 2023
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants