Request to change ClearOnDrop to hold ManuallyDrop<P> rather than P.#29
Open
emilHof wants to merge 3 commits into
Open
Request to change ClearOnDrop to hold ManuallyDrop<P> rather than P.#29emilHof wants to merge 3 commits into
ClearOnDrop to hold ManuallyDrop<P> rather than P.#29emilHof wants to merge 3 commits into
Conversation
ClearOnDrop to hold ManuallyDrop<P> rather than P.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
While I do not necessarily expect this to be merged, as this is maybe not the most urgent change, it does reduce the potential for UB in a couple of places.
The main reason for this change is the following original implementation:
When place is read into through a
*const Pit creates aUniqueretag of the underlying value, which gets invalidated by the consuming call ofmem::forget(c)as this causes a newUniqueretag. While this is not inherently UB there it does violate some safety invariants that make unsafe code more predictable.By changing
ClearOnDropto holdManuallyDrop<P>rather than justP, we avoid having to forgetcand thus need not invalided our borrow tag.There since now we do not forget our
ClearOnDrop, if we do not want our value to be cleared we zero out theManuallyDrop<P>as follows:This case is then checked for in the drop implementation as such:
These are the results of running
cargo benchon theub-drop-clearbranch:I totally understand if you do not see this as a necessary addition, I had fun working on it anyways! :D