try_lock already exists; it’s called lock. I just want a more convenient name and I want the name of the new method to be lock, but that ship has sailed.
I think a better solution would be to add a method called something like ulock that does a combined lock and unwrap.
My concern with lock+unwrap is only partly because of convenience; I also didn’t like it because I think it’s a bad idea to get people used to casually calling unwrap, because it tends to hide inadequate error handing.
Now that I think about it, I don’t like how unwrap can signal either “I know this can’t fail”, “the possible error states are too rare to care about” or “I can’t be bothered with real error handing right now”. In one or two of those cases you want to leave it in my production code, and in the last you want to audit all instances and replace them with proper error handing. Using the same function for all three cases makes that difficult.
a better solution would be to add a method called something like ulock that does a combined lock and unwrap.
That’s exactly what’s done above using an extension trait! You can mutex_val.ulock() with it!
Now that I think about it, I don’t like how unwrap can signal either “I know this can’t fail”, “the possible error states are too rare to care about” or “I can’t be bothered with real error handing right now”.
That’s why you’re told (clippy does that i think) to use expect instead, so you can signal “whatever string” you want to signal precisely.
try_lock
already exists; it’s calledlock
. I just want a more convenient name and I want the name of the new method to belock
, but that ship has sailed.if you’re really that bothered…
use std::sync::{Mutex, MutexGuard}; trait ULock<'a> { type Guard; fn ulock(&'a self) -> Self::Guard; } impl<'a, T: 'a> ULock<'a> for Mutex<T> { type Guard = MutexGuard<'a, T>; fn ulock(&'a self) -> Self::Guard { self.lock().unwrap() } }
or use a wrapper struct, if you really really want the method to be called exactly
lock
.I think a better solution would be to add a method called something like ulock that does a combined lock and unwrap.
My concern with lock+unwrap is only partly because of convenience; I also didn’t like it because I think it’s a bad idea to get people used to casually calling unwrap, because it tends to hide inadequate error handing.
Now that I think about it, I don’t like how unwrap can signal either “I know this can’t fail”, “the possible error states are too rare to care about” or “I can’t be bothered with real error handing right now”. In one or two of those cases you want to leave it in my production code, and in the last you want to audit all instances and replace them with proper error handing. Using the same function for all three cases makes that difficult.
That’s exactly what’s done above using an extension trait! You can
mutex_val.ulock()
with it!That’s why you’re told (clippy does that i think) to use
expect
instead, so you can signal “whatever string” you want to signal precisely.