Skip to content

Incompatible definition of Mutex::lock (or MutexGuard) #393

@Nahor

Description

@Nahor
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, 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 rather than T, then the Deref implementation needs to be corrected as well.

Footnotes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions