diff options
| author | Mica White <botahamec@gmail.com> | 2024-12-26 11:26:39 -0500 |
|---|---|---|
| committer | Mica White <botahamec@gmail.com> | 2024-12-26 12:06:47 -0500 |
| commit | dc16634f4abdb1e830d2749e64b419740702b302 (patch) | |
| tree | eb51ba8293a1c719c7221d546185cfa7062c108c /src/rwlock/rwlock.rs | |
| parent | 096afea6f13692fddbfad0b07e5377cb2e81dd58 (diff) | |
Commenting
Diffstat (limited to 'src/rwlock/rwlock.rs')
| -rw-r--r-- | src/rwlock/rwlock.rs | 65 |
1 files changed, 38 insertions, 27 deletions
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<T: Send, R: RawRwLock + Send + Sync> Lockable for RwLock<T, R> { } } +unsafe impl<T: Send, R: RawRwLock + Send + Sync> Sharable for RwLock<T, R> { + type ReadGuard<'g> + = RwLockReadRef<'g, T, R> + where + Self: 'g; + + unsafe fn read_guard(&self) -> Self::ReadGuard<'_> { + RwLockReadRef::new(self) + } +} + +unsafe impl<T: Send, R: RawRwLock + Send + Sync> OwnedLockable for RwLock<T, R> {} + impl<T: Send, R: RawRwLock + Send + Sync> LockableIntoInner for RwLock<T, R> { type Inner = T; @@ -107,19 +120,6 @@ impl<T: Send, R: RawRwLock + Send + Sync> LockableGetMut for RwLock<T, R> { } } -unsafe impl<T: Send, R: RawRwLock + Send + Sync> Sharable for RwLock<T, R> { - type ReadGuard<'g> - = RwLockReadRef<'g, T, R> - where - Self: 'g; - - unsafe fn read_guard(&self) -> Self::ReadGuard<'_> { - RwLockReadRef::new(self) - } -} - -unsafe impl<T: Send, R: RawRwLock + Send + Sync> OwnedLockable for RwLock<T, R> {} - impl<T, R: RawRwLock> RwLock<T, R> { /// Creates a new instance of an `RwLock<T>` which is unlocked. /// @@ -138,20 +138,6 @@ impl<T, R: RawRwLock> RwLock<T, R> { 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<T: ?Sized + Debug, R: RawRwLock> Debug for RwLock<T, R> { @@ -188,6 +174,9 @@ impl<T, R: RawRwLock> From<T> for RwLock<T, R> { } } +// 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<T: ?Sized, R> AsMut<T> for RwLock<T, R> { fn as_mut(&mut self) -> &mut T { self.data.get_mut() @@ -216,6 +205,28 @@ impl<T, R> RwLock<T, R> { } } +impl<T: ?Sized, R> RwLock<T, R> { + /// 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<T: ?Sized, R: RawRwLock> RwLock<T, R> { /// Locks this `RwLock` with shared read access, blocking the current /// thread until it can be acquired. |
