-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Tracking Issue for unsafe_cell_get_mut
#76943
Comments
This is small enough not to need an RFC, it seems, so it can go directly to the FCP: cc @RalfJung & the rest of |
FCP will happen when we move towards stabilization. We usually wait a few release cycles before doing that. Also this is T-libs territory, I don't think any language rules are being changed here (just clarified). @rust-lang/libs this is about adding However, there is a theoretical scenario where this can cause unsoundness in existing libraries: if a user-defined library, for some reason, exposes an |
Good point, although the only way they could rely on "not being to do anything with that reference" is if they (ab)used the fact that there was no other instance of type Otherwise it is possible to: fn mutate<T> (p: &mut UnsafeCell<T>, value: T)
{
*p = value.into();
} and more generally, we can currently write: fn with_mut<T, R> (
p: &'_ mut UnsafeCell<T>,
f: impl FnOnce(&'_ mut T) -> R,
) -> R
where
T : Default,
{
let mut prev = mem::take(p).into_inner();
// defer putting the value back
let mut prev = ::scopeguard::guard(prev, |it| *p = it.into());
f(&mut *prev)
}
|
Ah right, you can just overwrite what is in an |
…mut, r=RalfJung Add non-`unsafe` `.get_mut()` for `Unsafecell` - Tracking issue: rust-lang#76943 As discussed in: https://internals.rust-lang.org/t/add-non-unsafe-get-mut-for-unsafecell/12407 - ### [Rendered documentation](https://modest-dubinsky-1f9f47.netlify.app/core/cell/struct.unsafecell) This PR tries to move the sound `&mut UnsafeCell<T> -> &mut T` projection that all the "downstream" constructions were already relying on, up to the root abstraction, where it rightfully belongs, and officially blessing it. - this **helps reduce the amount of `unsafe` snippets out there** (_c.f._, the second commit of this PR: rust-lang@09503fd) The fact that this getter is now expose for `UnsafeCell<T>` itself, will also help convey the idea that **`UnsafeCell` is not magical _w.r.t._ `&mut` accesses**, contrary to what some people incorrectly think. - Even the standard library itself at some point had such a confusion, _c.f._ this comment where there is a mention of multi-threaded (and thus _shared_) access despite dealing with exclusive references over unique ownership: https://github.com/rust-lang/rust/blob/59fb88d061544a035f3043b47594b34789204cee/library/core/src/cell.rs#L498-L499 r? @RalfJung
…mut, r=RalfJung Add non-`unsafe` `.get_mut()` for `Unsafecell` - Tracking issue: rust-lang#76943 As discussed in: https://internals.rust-lang.org/t/add-non-unsafe-get-mut-for-unsafecell/12407 - ### [Rendered documentation](https://modest-dubinsky-1f9f47.netlify.app/core/cell/struct.unsafecell) This PR tries to move the sound `&mut UnsafeCell<T> -> &mut T` projection that all the "downstream" constructions were already relying on, up to the root abstraction, where it rightfully belongs, and officially blessing it. - this **helps reduce the amount of `unsafe` snippets out there** (_c.f._, the second commit of this PR: rust-lang@09503fd) The fact that this getter is now expose for `UnsafeCell<T>` itself, will also help convey the idea that **`UnsafeCell` is not magical _w.r.t._ `&mut` accesses**, contrary to what some people incorrectly think. - Even the standard library itself at some point had such a confusion, _c.f._ this comment where there is a mention of multi-threaded (and thus _shared_) access despite dealing with exclusive references over unique ownership: https://github.com/rust-lang/rust/blob/59fb88d061544a035f3043b47594b34789204cee/library/core/src/cell.rs#L498-L499 r? @RalfJung
…mut, r=RalfJung Add non-`unsafe` `.get_mut()` for `Unsafecell` - Tracking issue: rust-lang#76943 As discussed in: https://internals.rust-lang.org/t/add-non-unsafe-get-mut-for-unsafecell/12407 - ### [Rendered documentation](https://modest-dubinsky-1f9f47.netlify.app/core/cell/struct.unsafecell) This PR tries to move the sound `&mut UnsafeCell<T> -> &mut T` projection that all the "downstream" constructions were already relying on, up to the root abstraction, where it rightfully belongs, and officially blessing it. - this **helps reduce the amount of `unsafe` snippets out there** (_c.f._, the second commit of this PR: rust-lang@09503fd) The fact that this getter is now expose for `UnsafeCell<T>` itself, will also help convey the idea that **`UnsafeCell` is not magical _w.r.t._ `&mut` accesses**, contrary to what some people incorrectly think. - Even the standard library itself at some point had such a confusion, _c.f._ this comment where there is a mention of multi-threaded (and thus _shared_) access despite dealing with exclusive references over unique ownership: https://github.com/rust-lang/rust/blob/59fb88d061544a035f3043b47594b34789204cee/library/core/src/cell.rs#L498-L499 r? @RalfJung
…mut, r=RalfJung Add non-`unsafe` `.get_mut()` for `Unsafecell` - Tracking issue: rust-lang#76943 As discussed in: https://internals.rust-lang.org/t/add-non-unsafe-get-mut-for-unsafecell/12407 - ### [Rendered documentation](https://modest-dubinsky-1f9f47.netlify.app/core/cell/struct.unsafecell) This PR tries to move the sound `&mut UnsafeCell<T> -> &mut T` projection that all the "downstream" constructions were already relying on, up to the root abstraction, where it rightfully belongs, and officially blessing it. - this **helps reduce the amount of `unsafe` snippets out there** (_c.f._, the second commit of this PR: rust-lang@09503fd) The fact that this getter is now expose for `UnsafeCell<T>` itself, will also help convey the idea that **`UnsafeCell` is not magical _w.r.t._ `&mut` accesses**, contrary to what some people incorrectly think. - Even the standard library itself at some point had such a confusion, _c.f._ this comment where there is a mention of multi-threaded (and thus _shared_) access despite dealing with exclusive references over unique ownership: https://github.com/rust-lang/rust/blob/59fb88d061544a035f3043b47594b34789204cee/library/core/src/cell.rs#L498-L499 r? @RalfJung
Stabilization PR: #79485 |
…ut, r=m-ou-se Stabilize `unsafe_cell_get_mut` Tracking issue: rust-lang#76943 r? `@m-ou-se`
#79485 was merged 🎉 |
This is a tracking issue for adding the
fn get_mut (self: &mut UnsafeCell<T>) -> &mut T
method.The feature gate for the issue is
#![feature(unsafe_cell_get_mut)]
.History
unsafe
.get_mut()
forUnsafecell
#76936unsafe_cell_get_mut
#79485Unresolved Questions
None
The text was updated successfully, but these errors were encountered: