diff options
| author | Mica White <botahamec@gmail.com> | 2024-12-25 17:58:38 -0500 |
|---|---|---|
| committer | Mica White <botahamec@gmail.com> | 2024-12-25 22:44:03 -0500 |
| commit | 311842d28d1fbb6da945f0d98f1655f77a58abf9 (patch) | |
| tree | 4da3c8ab6fb3732c1ebb11306dab3799decd6b2c /src/collection/owned.rs | |
| parent | 37ab873d21ca1fcd43db8d6a26d5bac4f5285f71 (diff) | |
as_mut re-organization
Diffstat (limited to 'src/collection/owned.rs')
| -rw-r--r-- | src/collection/owned.rs | 87 |
1 files changed, 60 insertions, 27 deletions
diff --git a/src/collection/owned.rs b/src/collection/owned.rs index 714ff01..e4cfe46 100644 --- a/src/collection/owned.rs +++ b/src/collection/owned.rs @@ -1,6 +1,8 @@ use std::marker::PhantomData; -use crate::lockable::{Lockable, LockableIntoInner, OwnedLockable, RawLock, Sharable}; +use crate::lockable::{ + Lockable, LockableGetMut, LockableIntoInner, OwnedLockable, RawLock, Sharable, +}; use crate::Keyable; use super::{utils, LockGuard, OwnedLockCollection}; @@ -67,6 +69,17 @@ unsafe impl<L: Lockable> Lockable for OwnedLockCollection<L> { } } +impl<L: LockableGetMut> LockableGetMut for OwnedLockCollection<L> { + type Inner<'a> + = L::Inner<'a> + where + Self: 'a; + + fn get_mut(&mut self) -> Self::Inner<'_> { + self.data.get_mut() + } +} + impl<L: LockableIntoInner> LockableIntoInner for OwnedLockCollection<L> { type Inner = L::Inner; @@ -115,9 +128,15 @@ impl<E: OwnedLockable + Extend<L>, L: OwnedLockable> Extend<L> for OwnedLockColl } } -impl<L: OwnedLockable> AsMut<L> for OwnedLockCollection<L> { - fn as_mut(&mut self) -> &mut L { - &mut self.data +impl<T, L: AsRef<T>> AsRef<T> for OwnedLockCollection<L> { + fn as_ref(&self) -> &T { + self.data.as_ref() + } +} + +impl<T, L: AsMut<T>> AsMut<T> for OwnedLockCollection<L> { + fn as_mut(&mut self) -> &mut T { + self.data.as_mut() } } @@ -154,27 +173,6 @@ impl<L: OwnedLockable> OwnedLockCollection<L> { Self { data } } - /// Gets the underlying collection, consuming this collection. - /// - /// # Examples - /// - /// ``` - /// use happylock::{Mutex, ThreadKey}; - /// use happylock::collection::OwnedLockCollection; - /// - /// let data = (Mutex::new(42), Mutex::new("")); - /// let lock = OwnedLockCollection::new(data); - /// - /// let key = ThreadKey::get().unwrap(); - /// let inner = lock.into_inner(); - /// let guard = inner.0.lock(key); - /// assert_eq!(*guard, 42); - /// ``` - #[must_use] - pub fn into_inner(self) -> L { - self.data - } - /// Locks the collection /// /// This function returns a guard that can be used to access the underlying @@ -232,11 +230,11 @@ impl<L: OwnedLockable> OwnedLockCollection<L> { /// let lock = OwnedLockCollection::new(data); /// /// match lock.try_lock(key) { - /// Some(mut guard) => { + /// Ok(mut guard) => { /// *guard.0 += 1; /// *guard.1 = "1"; /// }, - /// None => unreachable!(), + /// Err(_) => unreachable!(), /// }; /// /// ``` @@ -397,6 +395,41 @@ impl<L: Sharable> OwnedLockCollection<L> { } } +impl<L> OwnedLockCollection<L> { + /// Gets the underlying collection, consuming this collection. + /// + /// # Examples + /// + /// ``` + /// use happylock::{Mutex, ThreadKey}; + /// use happylock::collection::OwnedLockCollection; + /// + /// let data = (Mutex::new(42), Mutex::new("")); + /// let lock = OwnedLockCollection::new(data); + /// + /// let key = ThreadKey::get().unwrap(); + /// let inner = lock.into_child(); + /// let guard = inner.0.lock(key); + /// assert_eq!(*guard, 42); + /// ``` + #[must_use] + pub fn into_child(self) -> L { + self.data + } +} + +impl<L: LockableGetMut> OwnedLockCollection<L> { + pub fn get_mut(&mut self) -> L::Inner<'_> { + LockableGetMut::get_mut(self) + } +} + +impl<L: LockableIntoInner> OwnedLockCollection<L> { + pub fn into_inner(self) -> L::Inner { + LockableIntoInner::into_inner(self) + } +} + #[cfg(test)] mod tests { use super::*; |
