sync.rs 553 B

12345678910111213141516171819
  1. //! Sync-related utilities.
  2. use std::sync::{Mutex, MutexGuard};
  3. /// Extension for [`Mutex`] to lock and panic if the lock could not be obtained.
  4. pub(crate) trait MutexPoisonPanic {
  5. type T;
  6. /// Acquires a mutex, blocking the current thread until it is able to do so, or panic if the lock is
  7. /// poisoned.
  8. fn lock_unwrap(&self) -> MutexGuard<'_, Self::T>;
  9. }
  10. impl<T> MutexPoisonPanic for Mutex<T> {
  11. type T = T;
  12. fn lock_unwrap(&self) -> MutexGuard<'_, Self::T> {
  13. self.lock().expect("lock should not be poisoned")
  14. }
  15. }