-
Notifications
You must be signed in to change notification settings - Fork 13.9k
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<T> { pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {…} }
impl<T: ?Sized> Arc<T> { pub unsafe fn get_mut_unchecked(this: &mut Self) -> &mut T {…} }Open questions / issues
- Rename to
get_unchecked_mutto match https://doc.rust-lang.org/std/?search=get_unchecked_mut ? - Arc::get_mut_unchecked does not mention restrictions on drop #136322