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.
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.
I don't really understand the goal of this change. It is also not correct; the interior mutability exception is entirely orthogonal to initialization.
Could you elaborate a bit more?
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.
Hm, maybe this change isn't helping to make things more clear...
This is roughly what I was doing:
But because
UnsafeCell::get
takes a reference, this would be invalid, right? There would temporary be a reference to an uninitializedUnsafeCell<bool>
.So I would think the exception that you can write through
MaybeUninit::as_ptr
when it wraps anUnsafeCell
would only be correct if it was already initialized.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.
No, the exception always applies. It's just you cannot make use of it through
UnsafeCell::get
. When writing unsafe code, you have to get all clauses of all constructs you use satisfied. Your example is fine as far asMaybeUninit::as_ptr
is concerned -- it it what you are doing later that breaks it. Under some proposed memory model, if you usetransmute
to turn*const UnsafeCell<bool>
into*mut bool
, that is allowed and then you may useptr:.write
to initialize thebool
.