From 311842d28d1fbb6da945f0d98f1655f77a58abf9 Mon Sep 17 00:00:00 2001 From: Mica White Date: Wed, 25 Dec 2024 17:58:38 -0500 Subject: as_mut re-organization --- src/collection/owned.rs | 87 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 27 deletions(-) (limited to 'src/collection/owned.rs') 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 Lockable for OwnedLockCollection { } } +impl LockableGetMut for OwnedLockCollection { + type Inner<'a> + = L::Inner<'a> + where + Self: 'a; + + fn get_mut(&mut self) -> Self::Inner<'_> { + self.data.get_mut() + } +} + impl LockableIntoInner for OwnedLockCollection { type Inner = L::Inner; @@ -115,9 +128,15 @@ impl, L: OwnedLockable> Extend for OwnedLockColl } } -impl AsMut for OwnedLockCollection { - fn as_mut(&mut self) -> &mut L { - &mut self.data +impl> AsRef for OwnedLockCollection { + fn as_ref(&self) -> &T { + self.data.as_ref() + } +} + +impl> AsMut for OwnedLockCollection { + fn as_mut(&mut self) -> &mut T { + self.data.as_mut() } } @@ -154,27 +173,6 @@ impl OwnedLockCollection { 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 OwnedLockCollection { /// 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 OwnedLockCollection { } } +impl OwnedLockCollection { + /// 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 OwnedLockCollection { + pub fn get_mut(&mut self) -> L::Inner<'_> { + LockableGetMut::get_mut(self) + } +} + +impl OwnedLockCollection { + pub fn into_inner(self) -> L::Inner { + LockableIntoInner::into_inner(self) + } +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3