MutexExt

Trait MutexExt 

Source
pub trait MutexExt<T> {
    // Required method
    fn lock_or_panic(&self) -> MutexGuard<'_, T>;
}
Expand description

Extension trait for Mutex to provide a method that acquires a lock, panicking if the lock is poisoned.

This helper function is intended to be used to avoid having to add many #[allow(clippy::unwrap_used)] annotations if there are a lot of usages of Mutex.

§Arguments

  • self - A reference to the Mutex to lock.

§Returns

A MutexGuard that provides access to the locked data.

§Panics

This function will panic if the Mutex is poisoned.

§Examples

use libdd_common::MutexExt;
use std::sync::{Arc, Mutex};

let data = Arc::new(Mutex::new(5));
let data_clone = Arc::clone(&data);

std::thread::spawn(move || {
    let mut num = data_clone.lock_or_panic();
    *num += 1;
})
.join()
.expect("Thread panicked");

assert_eq!(*data.lock_or_panic(), 6);

Required Methods§

Source

fn lock_or_panic(&self) -> MutexGuard<'_, T>

Implementations on Foreign Types§

Source§

impl<T> MutexExt<T> for Mutex<T>

Source§

fn lock_or_panic(&self) -> MutexGuard<'_, T>

Implementors§