patterns icon indicating copy to clipboard operation
patterns copied to clipboard

Incompatible definition of Mutex::lock (or MutexGuard)

Open Nahor opened this issue 11 months ago • 0 comments

struct MutexGuard<'a, T: 'a> {
    data: &'a T,
    //..
}

// Locking the mutex is explicit.
impl<T> Mutex<T> {
    fn lock(&self) -> MutexGuard<T> {
        // Lock the underlying OS mutex.
        //..

        // MutexGuard keeps a reference to self
        MutexGuard {
            data: self,
            //..
        }
    }
}

Some of the code implies that MutexGuard references Mutex<T>, but some other places imply that MutexGuard references T directly. I believe the correct definition of MutexGuard ^1 is

struct MutexGuard<'a, T: 'a> {
  data: &'a Mutex<T>,
  //..
}

And if MutexGuard contains a reference to Mutex<T> rather than T, then the Deref implementation needs to be corrected as well.

define lock as fn lock(&self) -> MutexGuard<Mutex<T>> or change the creation of the MutexGuard to MutexGuard { data: self.<unnamed field for now, ... }

Nahor avatar Mar 13 '24 20:03 Nahor