Why `UnsafeCell` doesn't implement `AsMut`

UnsafeCell (as well as Cell/RefCell) has a method pub fn get_mut(&mut self) -> &mut T, which has exactly the same signature than AsMut::as_mut.

I've found Add non-`unsafe` `get_mut` for `UnsafeCell` and https://github.com/rust-lang/rust/pull/76936, but AsMut::as_mut was neither mentioned in the post, nor in the PR.

Is there a reason to have a dedicated method? Would it not be simpler to implement AsMut for cell types? May I open a PR to add AsMut implementation? Should get_mut be deprecated in favor of AsMut:::as_mut

For the context, I would like to also implement AsMut for loom::cell::UnsafeCell, see my comment in UnsafeCell::get_mut() is different from std · Issue #277 · tokio-rs/loom · GitHub

1 Like

I don't know the historical reason, but on general principle: UnsafeCell is something where you want to think very carefully about each operation, and passing it to generic code is not really that kind of care.

2 Likes

I wonder if there should be some mention of AsRef/AsMut in the docs explaining this reasoning.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.