-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Clarify UnsafeCell docs; fix #34496 #34520
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
Conversation
r? @gankro or @steveklabnik |
/// that `&mut T` is unique. When building abstractions like `Cell`, `RefCell`, `Mutex`, etc, | ||
/// you need to turn these optimizations off. `UnsafeCell` is the way to do this, | ||
/// when `UnsafeCell<T>` is immutably aliased, it is still safe to obtain a mutable | ||
/// reference to its interior and/or to mutate it. However, it us up to the abstraction designer |
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.
s/us/is
a2c9205
to
03660fd
Compare
updated |
👍 |
While these docs are being improved, could you mention that an UnsafeCell itself should never be mutable or you break its key functionality - #16022 |
I'm not quite sure what this means. It is possible to, for example, nest RefCells; e.g. RefCell<Vec<RefCell>>, giving you an &mut to a RefCell. |
I don't see that issue being a problem, really. &mut is unique, even if there is an unsafecell behind it. UnsafeCell is not meant to be used to make &mut shareable, it is to be used to make & mutable. There is no key functionality being broken, that's &mut working as intended. |
Yes, but this may initially not be expected given the documentation has just talked about how UnsafeCell is special and lets you use mutably aliased data - reinforcing that it is not special when the UnsafeCell itself is mutably aliased doesn't hurt. Concrete example:
1.7.0 (the last version before |
If you're not convinced then don't worry about it and I'll make a separate PR at some point. |
efac72b
to
1fe8f9c
Compare
Fixed |
/// from the cell. This is often done via runtime checks. | ||
/// | ||
/// Note that while mutating or mutably aliasing the contents of an `& UnsafeCell<T>` is | ||
/// okay (provided you enforce the invariants some othe way); it is still undefined behavior |
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.
Typo: should be "other"
@Manishearth just a few nits, and this PR is good to go |
1fe8f9c
to
3873402
Compare
@bors r=steveklabnik fixed nits |
📌 Commit 3873402 has been approved by |
⌛ Testing commit 3873402 with merge 1d588dc... |
💔 Test failed - auto-win-msvc-64-opt-rustbuild |
@bors retry |
⌛ Testing commit 3873402 with merge ccd5daf... |
💔 Test failed - auto-win-msvc-64-opt |
@bors: retry |
Clarify UnsafeCell docs; fix #34496 None
⛄ The build was interrupted to prioritize another pull request. |
Clarify UnsafeCell docs; fix #34496 None
No description provided.