Tracking issue for {Rc, Arc}::get_mut_unchecked · Issue #63292 · rust-lang/rust (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Description
On Rc
and Arc
an new unsafe get_mut_unchecked
method provides &mut T
access without checking the reference count. Arc::get_mut
involves multiple atomic operations whose cost can be non-trivial. Rc::get_mut
is less costly, but we add Rc::get_mut_unchecked
anyway for symmetry with Arc
.
These can be useful independently, but they will presumably be typical when uninitialized constructors (tracked in #63291) are used.
An alternative with a safe API would be to introduce UniqueRc
and UniqueArc
types that have the same memory layout as Rc
and Arc
(and so zero-cost conversion to them) but are guaranteed to have only one reference. But introducing entire new types feels “heavier” than new constructors on existing types, and initialization of MaybeUninit<T>
typically requires unsafe code anyway.
PR #62451 adds:
impl<T: ?Sized> Rc { pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {…} } impl<T: ?Sized> Arc { pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {…} }
Open questions / issues
- Rename to
get_unchecked_mut
to match https://doc.rust-lang.org/std/?search=get_unchecked_mut ? - Arc::get_mut_unchecked does not mention restrictions on drop #136322