Creative methods on interior mutability types
In the previous post Interior mutability patterns we looked at four different conventions that can be used to safely use interor mutability. We didn’t look further than the basic API, that was already plenty of material to cover.
Yet there are some creative methods that push the limit of those abstractions. Let’s explore them!
On container types:
method | Cell |
Atomic ¹ |
AtomicCell ¹ |
RefCell |
Mutex |
RwLock |
OnceCell |
QCell |
TCell |
LCell |
---|---|---|---|---|---|---|---|---|---|---|
into_inner |
yes | yes | yes | yes | yes | yes | yes | yes* | yes* | yes* |
get_mut |
yes | — | yes* | yes | yes | yes | yes | yes* | yes* | yes* |
as_ptr |
yes | yes | yes | yes | yes | yes | yes* | yes | yes | yes |
from_mut |
yes | — | yes* | (BoCell ) |
— | — | — | — | yes* | yes* |
replace |
yes | yes | yes | yes | — | — | — | yes* | yes* | yes* |
swap |
yes | yes | yes | yes | — | — | — | — | — | — |
update |
yes | — | — | yes | — | — | — | —* | —* | —* |
as_slice_of_cells |
yes | — | — | — | — | — | — | — | yes* | yes* |
with |
— | — | — | — | maybe | maybe | — | — | — | — |
On smart pointers:
method | Ref |
RefMut |
MutexGuard |
RwLockReadGuard |
RwLockWriteGuard |
ReentrantMutexGuard |
---|---|---|---|---|---|---|
bump |
— | — | yes* | yes* | yes* | yes |
unlocked |
— | — | yes* | yes* | yes* | yes |
Condvar::wait |
— | — | yes | yes* | yes* | yes* |
map |
yes | yes | yes* | yes* | yes* | yes |
map_split |
yes | yes | yes* | yes* | yes* | yes* |
try_map |
yes* | yes* | yes* | yes* | yes* | yes |
downgrade |
— | — | — | — | yes* | — |
upgrade |
yes* | — | — | maybe | — | — |
¹: In this post I use Atomic<T>
to describe a wrapper based on atomic operations, and AtomicCell<T>
to describe a lock-based solution for larger types.
*: possible, but not implemented (or not implemented in the standard library or main crate)