-
Notifications
You must be signed in to change notification settings - Fork 20
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
UnsafeCell
access APIs
#521
Comments
We discussed this in the libs-api meeting. We're generally happy to add convenience methods to As such we're accepting this ACP with a slight change: impl<T> UnsafeCell<T> {
/// Replace the value in this `UnsafeCell` and return the old value.
unsafe fn replace(&self, new_value: T) -> T;
/// Get a shared reference to the wrapped value.
unsafe fn get_ref_unchecked(&self) -> &T;
/// Get a mutable reference to the wrapped value.
unsafe fn get_mut_unchecked(&self) -> &mut T;
} |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Should these be named |
add UnsafeCell direct access APIs - Implementation for ACP: rust-lang/libs-team#521 - Tracking issue rust-lang#136327
Rollup merge of rust-lang#136398 - pitaj:unsafecell_access, r=dtolnay add UnsafeCell direct access APIs - Implementation for ACP: rust-lang/libs-team#521 - Tracking issue rust-lang#136327
Proposal
Problem statement
In the wild, the following patterns are pretty common:
These operation involve raw pointer dereferences. Many users of Rust prefer to avoid raw pointers if at all possible, do to the numerous invariants that must be upheld to avoid UB (non-null, aligned, plus initialized in the case of assignment).
If you already have
&UnsafeCell
, why should you be required to go through a raw pointer just to assign to or take a reference to the value within?Motivating examples or use cases
Consider this example from once_cell, which splits the
.get()
from the assignment:If you were to audit the lower unsafe block in this example:
The safety comment on the function itself handles the synchronization, but you would have to look at context to see that it is derived from the
UnsafeCell
, and therefore fulfills the following invariants:If safety comments were required, you might see a comment like this:
// SAFETY: Pointer is valid and old value is initialized, because it comes from the `UnsafeCell` above.
Which would be unnecessary if just an
&UnsafeCell
reference could be used instead.Solution sketch
The above added APIs allow us to rewrite the
once_cell
code like this:And more generally, users can use references instead of raw pointers in more places:
&UnsafeCell<T>
does not currently have a pointer equivalent&UnsafeCell<MaybeUninit<T>>
roughly equivalent toNonNull<T>
Option<&UnsafeCell<MaybeUninit<T>>>
roughly equivalent to*mut T
Alternatives
Do nothing and people continue to just use the raw pointer operations.
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution:
The text was updated successfully, but these errors were encountered: