reference
reference copied to clipboard
Confusion about whether union writes ever have to be placed in `unsafe` blocks
https://doc.rust-lang.org/reference/items/unions.html says:
Writes to
CopyorManuallyDropunion fields do not require reads for running destructors, so these writes don't have to be placed inunsafeblocks
but later also says:
When a union is dropped, it cannot know which of its fields needs to be dropped. For this reason, all union fields must either be of a
Copytype or of the shapeManuallyDrop<_>.
If the latter is true, then the former seems to be written in an overly-qualified way.
Yea, I can see how that seems a little awkward. I think this is a result of the evolution of unions, where the original RFC allowed drop types, and the way it was phrased then was a little different:
Writes to union fields may generally require reads (for running destructors), so these writes have to be placed in unsafe blocks too.
That chapter definitely could use a little cleanup to more clearly state up-front the restrictions on the fields. Regarding the "unsafe" aspect, that could be reworded to be more of an explanation of why writes are safe, to contrast with reads.