loom icon indicating copy to clipboard operation
loom copied to clipboard

UnsafeCell::get_mut() is different from std

Open kvark opened this issue 3 years ago • 5 comments

In std, it gets &mut self. In loom, it gets &self. I think it's not a good idea to diverge in this way. I understand the return type may be different, but &self should be borrowed in the same way as it is in std.

kvark avatar Jun 06 '22 06:06 kvark

Ah, well, the get_mut function in loom was never intended to be the loom version of the standard library function of the same name. It is intended to be the loom version of UnsafeCell::get in the cases where you want to modify the value. (And our get is the version of UnsafeCell::get for situations where you only read the value.)

Darksonn avatar Jun 06 '22 08:06 Darksonn

Ok, if the intent was so different, let's have a different name to avoid confusion. As far as I see, loom is generally used as a replacement for std, so having a difference in semantics like this is not desirable.

kvark avatar Jun 07 '22 07:06 kvark

it's worth noting that the type was previously named CausalCell, but was renamed to UnsafeCell in #111. here's Carl's rationale at the time: https://github.com/tokio-rs/loom/pull/111#discussion_r391882249

hawkw avatar Jun 07 '22 20:06 hawkw

I don't think this api is that bad. Since std::cell::UnsafeCell::get can be used to reimplement std::cell::UnsafeCell::get_mut, you can use our get_mut both as a get and get_mut.

Darksonn avatar Jun 07 '22 20:06 Darksonn

Should it be to add a method equivalent to std::cell::UnsafeCell::get_mut in loom::cell::UnsafeCell? std get_mut is normally safe to call (because of exclusive reference), but calling loom equivalent requires unsafe code, while it could be avoided.

I've just written this post about implementing AsMut for cell types; could loom::cell::UnsafeCell also implement AsMut?

wyfo avatar Jul 12 '23 21:07 wyfo