4 ms·
Using a mutex basically requires the use of unwrap let lockedValue = mutex.lock().unwrap(); The only time .lock() can fail is if another thread panicked and p
by assbuttbuttass 4y ago
Using a mutex basically requires the use of unwrap
let lockedValue = mutex.lock().unwrap();
The only time .lock() can fail is if another thread panicked and poisoned the mutex, so propagating the panic makes the most sense here.
- marcosdumay 4y agoLooks like an expect there would be an improvement. Do you write that line often enough that adding an error message is a problem?
- assbuttbuttass 4y agoThe reason I would hesitate to replace this with an expect is that I really don't ever expect .lock() to fail, so .expect just adds verbosity for no benefit. The article talks about the Regex::new method, which has much the same problem. For the majority of use cases, you don't expect it to ever fail. Maybe Mutex could introduce a new method .lock_or_die, because it's such a common pattern
- Matthias247 4y agoThere's not really a benefit to "except", since the stacktrace you get on the panic tells you exactly what happened (Mutex was poisoned). Repeating that in a custom error string doesn't make the behavior much better.