Today it is possible to call
mutex.lock()?.deref_mut() to get a mutable reference to the contents of the mutex while holding the lock.
It is also possible to call
mutex.get_mut()? to get a mutable reference to the contents without aquiring the lock, if the borrow checker can prove no other code could be doing this at the same time.
My question is: Is it possible to create a method or macro that would allow a single syntax to do either of these depending on whether the caller can mutably borrow the mutex? This way the lock is optimized out when it is not needed without the caller thinking about it.
If this is not possible today, is there some feature that could enable this?