rust-lang / rfcs

RFCs for changes to Rust
https://rust-lang.github.io/rfcs/
Apache License 2.0
5.9k stars 1.56k forks source link

Making `MutexGuard`, `RwLockReadGuard` & `RwLockWriteGuard` more generic #3377

Open Aandreba opened 1 year ago

Aandreba commented 1 year ago

Currently, the locks guards are defined like this:

pub struct MutexGuard<'a, T: ?Sized + 'a> {
    lock: &'a Mutex<T>,
    poison: poison::Guard,
}

But there are a lot of instances where taking a reference to the lock is not as versatile as it could be, so perhaps a redefinition of the locks to something like this makes more sense.

pub struct OwnedMutexGuard<M: Deref<Target = Mutex<T>>, T: ?Sized> {
    lock: M,
    poison: poison::Guard,
}

pub type MutexGuard<'a, T> = OwnedMutexGuard<&'a Mutex<T>, T>
SOF3 commented 1 year ago

Are you looking for something like ArcMutexGuard? You might want to use the parking_lot implementations instead.

Aandreba commented 1 year ago

I know of parking_lot, but their implementation of the locks is completely different, and alos their owned locks are restricted to Arcs. In the case I want to get the lock from an Rc, Ref (a RefCell guard) or any other dereferencing type, I'll be forced to get it by reference.