embassy-sync: add lock_mut to blocking_mutex::Mutex#3978
embassy-sync: add lock_mut to blocking_mutex::Mutex#3978lulf merged 2 commits intoembassy-rs:mainfrom
lock_mut to blocking_mutex::Mutex#3978Conversation
|
My only reservation here is that having this API we could be promoting a bad pattern of using lock_mut instead of the safer RefCell wrap, even if it's marked unsafe. At a minimum it should perhaps recommend using a RefCell in the docs to point the users towards that. |
|
I'll update the doc comment to point to the safer pattern of using a You'd need to do something like this to shoot yourself in the foot: unsafe {
MUTEX.lock_mut(|value| {
MUTEX.lock_mut(|value| /* 💥 */)
})
} For a blocking mutex that can't be locked across await points it seems very unlikely that someone will overlook this. That said, I also don't like the unsafety of this. I mostly added this to avoid the boilerplate of using a |
016534f to
73b4fae
Compare
73b4fae to
7a031ee
Compare
This adds a
lock_mutmethod toblocking_mutex::Mutexin order to get direct mutable access to the protected data. This removes the need for an extraRefCellwhen mutable access is needed.The method is marked
unsafebecause calling this method when the mutex is already locked causes UB. Other than that this implementation should be sound.