Request to change ClearOnDrop
to hold ManuallyDrop<P>
rather than P
.
#29
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 P
it creates aUnique
retag of the underlying value, which gets invalidated by the consuming call ofmem::forget(c)
as this causes a newUnique
retag. While this is not inherently UB there it does violate some safety invariants that make unsafe code more predictable.By changing
ClearOnDrop
to holdManuallyDrop<P>
rather than justP
, we avoid having to forgetc
and 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 bench
on theub-drop-clear
branch:I totally understand if you do not see this as a necessary addition, I had fun working on it anyways! :D