Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Nahor opened this issue Mar 13, 2024 · 0 comments
Open

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

Nahor opened this issue Mar 13, 2024 · 0 comments

Comments

@Nahor
Copy link

Nahor commented Mar 13, 2024

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, ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant