From dc16634f4abdb1e830d2749e64b419740702b302 Mon Sep 17 00:00:00 2001 From: Mica White Date: Thu, 26 Dec 2024 11:26:39 -0500 Subject: Commenting --- src/rwlock/rwlock.rs | 65 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 27 deletions(-) (limited to 'src/rwlock/rwlock.rs') diff --git a/src/rwlock/rwlock.rs b/src/rwlock/rwlock.rs index 8bb170c..6432128 100644 --- a/src/rwlock/rwlock.rs +++ b/src/rwlock/rwlock.rs @@ -88,6 +88,19 @@ unsafe impl Lockable for RwLock { } } +unsafe impl Sharable for RwLock { + type ReadGuard<'g> + = RwLockReadRef<'g, T, R> + where + Self: 'g; + + unsafe fn read_guard(&self) -> Self::ReadGuard<'_> { + RwLockReadRef::new(self) + } +} + +unsafe impl OwnedLockable for RwLock {} + impl LockableIntoInner for RwLock { type Inner = T; @@ -107,19 +120,6 @@ impl LockableGetMut for RwLock { } } -unsafe impl Sharable for RwLock { - type ReadGuard<'g> - = RwLockReadRef<'g, T, R> - where - Self: 'g; - - unsafe fn read_guard(&self) -> Self::ReadGuard<'_> { - RwLockReadRef::new(self) - } -} - -unsafe impl OwnedLockable for RwLock {} - impl RwLock { /// Creates a new instance of an `RwLock` which is unlocked. /// @@ -138,20 +138,6 @@ impl RwLock { raw: R::INIT, } } - - /// Returns the underlying raw reader-writer lock object. - /// - /// Note that you will most likely need to import the [`RawRwLock`] trait - /// from `lock_api` to be able to call functions on the raw reader-writer - /// lock. - /// - /// # Safety - /// - /// This method is unsafe because it allows unlocking a mutex while - /// still holding a reference to a lock guard. - pub const unsafe fn raw(&self) -> &R { - &self.raw - } } impl Debug for RwLock { @@ -188,6 +174,9 @@ impl From for RwLock { } } +// We don't need a `get_mut` because we don't have mutex poisoning. Hurray! +// This is safe because you can't have a mutable reference to the lock if it's +// locked. Being locked requires an immutable reference because of the guard. impl AsMut for RwLock { fn as_mut(&mut self) -> &mut T { self.data.get_mut() @@ -216,6 +205,28 @@ impl RwLock { } } +impl RwLock { + /// Returns a mutable reference to the underlying data. + /// + /// Since this call borrows `RwLock` mutably, no actual locking is taking + /// place. The mutable borrow statically guarantees that no locks exist. + /// + /// # Examples + /// + /// ``` + /// use happylock::{ThreadKey, RwLock}; + /// + /// let key = ThreadKey::get().unwrap(); + /// let mut mutex = RwLock::new(0); + /// *mutex.get_mut() = 10; + /// assert_eq!(*mutex.read(key), 10); + /// ``` + #[must_use] + pub fn get_mut(&mut self) -> &mut T { + self.data.get_mut() + } +} + impl RwLock { /// Locks this `RwLock` with shared read access, blocking the current /// thread until it can be acquired. -- cgit v1.2.3