jadesrandomutil 0.1.0

A random util library for Rust for Jade
Documentation
use std::sync::*;



pub trait EasyAccessMutex<T> {
    /// Get a reference to the value inside the mutex.
    fn get(&self) -> MutexGuard<T>;

    /// Perform an operation on the value inside the mutex.
    fn with<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut T) -> R;
}

impl<T> EasyAccessMutex<T> for Mutex<T> {
    fn get(&self) -> MutexGuard<T> {
        self.lock().expect("Mutex is poisoned")
    }

    fn with<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut T) -> R,
    {
        let mut guard = self.lock().expect("Mutex is poisoned");
        f(&mut *guard)
    }
}