Skip to content

Commit 0b71083

Browse files
chore: Make RwLockReadGuard covariant (#45)
Closes #9 by making the RwLockReadGuard type covariant. This involves splitting the RwLock implementation into a "raw" RwLock part and the public API.
1 parent 99a44a5 commit 0b71083

File tree

4 files changed

+768
-392
lines changed

4 files changed

+768
-392
lines changed

src/mutex.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ impl<T: ?Sized> Mutex<T> {
163163
pub fn get_mut(&mut self) -> &mut T {
164164
unsafe { &mut *self.data.get() }
165165
}
166+
167+
/// Unlocks the mutex directly.
168+
///
169+
/// # Safety
170+
///
171+
/// This function is intended to be used only in the case where the mutex is locked,
172+
/// and the guard is subsequently forgotten. Calling this while you don't hold a lock
173+
/// on the mutex will likely lead to UB.
174+
pub(crate) unsafe fn unlock_unchecked(&self) {
175+
// Remove the last bit and notify a waiting lock operation.
176+
self.state.fetch_sub(1, Ordering::Release);
177+
self.lock_ops.notify(1);
178+
}
166179
}
167180

168181
impl<T: ?Sized> Mutex<T> {
@@ -562,10 +575,12 @@ impl<'a, T: ?Sized> MutexGuard<'a, T> {
562575
}
563576

564577
impl<T: ?Sized> Drop for MutexGuard<'_, T> {
578+
#[inline]
565579
fn drop(&mut self) {
566-
// Remove the last bit and notify a waiting lock operation.
567-
self.0.state.fetch_sub(1, Ordering::Release);
568-
self.0.lock_ops.notify(1);
580+
// SAFETY: we are dropping the mutex guard, therefore unlocking the mutex.
581+
unsafe {
582+
self.0.unlock_unchecked();
583+
}
569584
}
570585
}
571586

@@ -623,10 +638,12 @@ impl<T: ?Sized> MutexGuardArc<T> {
623638
}
624639

625640
impl<T: ?Sized> Drop for MutexGuardArc<T> {
641+
#[inline]
626642
fn drop(&mut self) {
627-
// Remove the last bit and notify a waiting lock operation.
628-
self.0.state.fetch_sub(1, Ordering::Release);
629-
self.0.lock_ops.notify(1);
643+
// SAFETY: we are dropping the mutex guard, therefore unlocking the mutex.
644+
unsafe {
645+
self.0.unlock_unchecked();
646+
}
630647
}
631648
}
632649

0 commit comments

Comments
 (0)