-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add a conversion from &mut T
to &mut UnsafeCell<T>
#198
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Comments
JoJoJet
added
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
labels
Mar 29, 2023
I have an initial implementation at rust-lang/rust#107917. |
do note that |
Of course. |
james7132
pushed a commit
to bevyengine/bevy
that referenced
this issue
Mar 31, 2023
# Objective The function `SyncUnsafeCell::from_mut` returns `&SyncUnsafeCell<T>`, even though it could return `&mut SyncUnsafeCell<T>`. This means it is not possible to call `get_mut` on the returned value, so you need to use unsafe code to get exclusive access back. ## Solution Return `&mut Self` instead of `&Self` in `SyncUnsafeCell::from_mut`. This is consistent with my proposal for `UnsafeCell::from_mut`: rust-lang/libs-team#198. Replace an unsafe pointer dereference with a safe call to `get_mut`. --- ## Changelog + The function `bevy_utils::SyncUnsafeCell::get_mut` now returns a value of type `&mut SyncUnsafeCell<T>`. Previously, this returned an immutable reference. ## Migration Guide The function `bevy_utils::SyncUnsafeCell::get_mut` now returns a value of type `&mut SyncUnsafeCell<T>`. Previously, this returned an immutable reference.
joshtriplett
added
the
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
label
May 16, 2023
The library team discussed this, and we think this API makes sense. Approved! |
This was referenced May 16, 2023
Dylan-DPC
added a commit
to Dylan-DPC/rust
that referenced
this issue
May 17, 2023
…r=joshtriplett Add a conversion from `&mut T` to `&mut UnsafeCell<T>` Provides a safe way of downgrading an exclusive reference into an alias-able `&UnsafeCell<T>` reference. ACP: rust-lang/libs-team#198.
thomcc
pushed a commit
to tcdi/postgrestd
that referenced
this issue
Jul 18, 2023
…plett Add a conversion from `&mut T` to `&mut UnsafeCell<T>` Provides a safe way of downgrading an exclusive reference into an alias-able `&UnsafeCell<T>` reference. ACP: rust-lang/libs-team#198.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
Problem statement
There should be a safe API that converts an
&mut T
reference into an&mut UnsafeCell<T>
reference within a certain scope.Motivation, use-cases
When dealing with exclusive references, sometimes you may wish to opt in to temporary shared mutable access. If your type implements
Copy
, you can useCell::from_mut
to convert a mutable reference to aCell
, which allows interior mutability. For non-Copy
types, it is often required to useUnsafeCell
to allow interior mutability. Converting from&mut T
to&mut UnsafeCell<T>
is always sound, however there is currently no safe way to do this so the user is required to usestd::mem::transmute
. Use of this function is potentially error-prone, as a simple mistake might result in the returned reference having an incorrect lifetime.For an example of a situation where this would be useful, consider this code I wrote for the bevy game engine:
Solution sketches
We should add an associated fn to the
UnsafeCell
type:This returns
&mut UnsafeCell<T>
, unlikeCell::from_mut
which returns&Cell
. It was a mistake forCell::from_mut
to work this way, since exclusive references are more useful due to the{Unsafe}Cell::get_mut
function.Links and related work
What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: