5 ms·
From the documentation [0] for Mutex<T>: For a mutex, this means that the lock and try_lock methods return a Result which indicates whether a mutex has b
by therein 9y ago
From the documentation [0] for Mutex<T>:
For a mutex, this means that the lock and try_lock
methods return a Result which indicates whether a
mutex has been poisoned or not. Most usage of a mutex
will simply unwrap() these results, propagating panics
among threads to ensure that a possibly invalid
invariant is not witnessed.
A poisoned mutex, however, does not prevent all access
to the underlying data. The PoisonError type has an
into_inner method which will return the guard that
would have otherwise been returned on a successful
lock. This allows access to the data, despite the
lock being poisoned.
[0] https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html
- millstone 9y agoThe author argues that explicit unwrap() is good because it forces you to realize the value may be empty and think "Oh crap, I better handle that too." But the docs you linked say that "most usage of a mutex will simply unwrap these results" and not try to handle the case. So it seems like the explicit unwrap() here is not preventing any bugs, it's just adding noise. Wouldn't automatic unwrap be better, at least in this case?
- iknowstuff 9y agoAgain, it makes the programmer aware of possible issues and gives them a nudge to consider handling the error. This is very valuable.