matklad / once_cell

Rust library for single assignment cells and lazy statics without macros

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nit: unnecessary synchronization in `Lazy::force_mut()`

danielhenrymantilla opened this issue · comments

Self::force() has to synchronize the access to the raw value and/or its needed initialization, given its &-based API, but force_mut() does not, thanks to the guarantees of &mut access (e.g., https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html#method.get_mut).

But the current implementation of force_mut() just defers to force():

once_cell/src/lib.rs

Lines 767 to 770 in 3514863

pub fn force_mut(this: &mut Lazy<T, F>) -> &mut T {
Self::force(this);
Self::get_mut(this).unwrap_or_else(|| unreachable!())
}

Yup, this indeed should be improved!