diff options
| author | Botahamec <botahamec@outlook.com> | 2025-02-28 16:09:11 -0500 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2025-02-28 16:09:11 -0500 |
| commit | 4ba03be97e6cc7e790bbc9bfc18caaa228c8a262 (patch) | |
| tree | a257184577a93ddf240aba698755c2886188788b /src/collection/guard.rs | |
| parent | 4a5ec04a29cba07c5960792528bd66b0f99ee3ee (diff) | |
Scoped lock API
Diffstat (limited to 'src/collection/guard.rs')
| -rw-r--r-- | src/collection/guard.rs | 83 |
1 files changed, 25 insertions, 58 deletions
diff --git a/src/collection/guard.rs b/src/collection/guard.rs index eea13ed..78d9895 100644 --- a/src/collection/guard.rs +++ b/src/collection/guard.rs @@ -2,41 +2,11 @@ use std::fmt::{Debug, Display}; use std::hash::Hash; use std::ops::{Deref, DerefMut}; -use crate::key::Keyable; - use super::LockGuard; -#[mutants::skip] // it's hard to get two guards safely -#[cfg(not(tarpaulin_include))] -impl<Guard: PartialEq, Key: Keyable> PartialEq for LockGuard<'_, Guard, Key> { - fn eq(&self, other: &Self) -> bool { - self.guard.eq(&other.guard) - } -} - -#[mutants::skip] // it's hard to get two guards safely -#[cfg(not(tarpaulin_include))] -impl<Guard: PartialOrd, Key: Keyable> PartialOrd for LockGuard<'_, Guard, Key> { - fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { - self.guard.partial_cmp(&other.guard) - } -} - -#[mutants::skip] // it's hard to get two guards safely -#[cfg(not(tarpaulin_include))] -impl<Guard: Eq, Key: Keyable> Eq for LockGuard<'_, Guard, Key> {} - -#[mutants::skip] // it's hard to get two guards safely -#[cfg(not(tarpaulin_include))] -impl<Guard: Ord, Key: Keyable> Ord for LockGuard<'_, Guard, Key> { - fn cmp(&self, other: &Self) -> std::cmp::Ordering { - self.guard.cmp(&other.guard) - } -} - #[mutants::skip] // hashing involves RNG and is hard to test #[cfg(not(tarpaulin_include))] -impl<Guard: Hash, Key: Keyable> Hash for LockGuard<'_, Guard, Key> { +impl<Guard: Hash> Hash for LockGuard<Guard> { fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.guard.hash(state) } @@ -44,19 +14,19 @@ impl<Guard: Hash, Key: Keyable> Hash for LockGuard<'_, Guard, Key> { #[mutants::skip] #[cfg(not(tarpaulin_include))] -impl<Guard: Debug, Key: Keyable> Debug for LockGuard<'_, Guard, Key> { +impl<Guard: Debug> Debug for LockGuard<Guard> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Debug::fmt(&**self, f) } } -impl<Guard: Display, Key: Keyable> Display for LockGuard<'_, Guard, Key> { +impl<Guard: Display> Display for LockGuard<Guard> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Display::fmt(&**self, f) } } -impl<Guard, Key: Keyable> Deref for LockGuard<'_, Guard, Key> { +impl<Guard> Deref for LockGuard<Guard> { type Target = Guard; fn deref(&self) -> &Self::Target { @@ -64,19 +34,19 @@ impl<Guard, Key: Keyable> Deref for LockGuard<'_, Guard, Key> { } } -impl<Guard, Key: Keyable> DerefMut for LockGuard<'_, Guard, Key> { +impl<Guard> DerefMut for LockGuard<Guard> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.guard } } -impl<Guard, Key: Keyable> AsRef<Guard> for LockGuard<'_, Guard, Key> { +impl<Guard> AsRef<Guard> for LockGuard<Guard> { fn as_ref(&self) -> &Guard { &self.guard } } -impl<Guard, Key: Keyable> AsMut<Guard> for LockGuard<'_, Guard, Key> { +impl<Guard> AsMut<Guard> for LockGuard<Guard> { fn as_mut(&mut self) -> &mut Guard { &mut self.guard } @@ -97,56 +67,53 @@ mod tests { #[test] fn deref_mut_works() { - let mut key = ThreadKey::get().unwrap(); + let key = ThreadKey::get().unwrap(); let locks = (Mutex::new(1), Mutex::new(2)); let lock = LockCollection::new_ref(&locks); - let mut guard = lock.lock(&mut key); + let mut guard = lock.lock(key); *guard.0 = 3; - drop(guard); + let key = LockCollection::<(Mutex<_>, Mutex<_>)>::unlock(guard); - let guard = locks.0.lock(&mut key); + let guard = locks.0.lock(key); assert_eq!(*guard, 3); - drop(guard); + let key = Mutex::unlock(guard); - let guard = locks.1.lock(&mut key); + let guard = locks.1.lock(key); assert_eq!(*guard, 2); - drop(guard); } #[test] fn as_ref_works() { - let mut key = ThreadKey::get().unwrap(); + let key = ThreadKey::get().unwrap(); let locks = (Mutex::new(1), Mutex::new(2)); let lock = LockCollection::new_ref(&locks); - let mut guard = lock.lock(&mut key); + let mut guard = lock.lock(key); *guard.0 = 3; - drop(guard); + let key = LockCollection::<(Mutex<_>, Mutex<_>)>::unlock(guard); - let guard = locks.0.lock(&mut key); + let guard = locks.0.lock(key); assert_eq!(guard.as_ref(), &3); - drop(guard); + let key = Mutex::unlock(guard); - let guard = locks.1.lock(&mut key); + let guard = locks.1.lock(key); assert_eq!(guard.as_ref(), &2); - drop(guard); } #[test] fn as_mut_works() { - let mut key = ThreadKey::get().unwrap(); + let key = ThreadKey::get().unwrap(); let locks = (Mutex::new(1), Mutex::new(2)); let lock = LockCollection::new_ref(&locks); - let mut guard = lock.lock(&mut key); + let mut guard = lock.lock(key); let guard_mut = guard.as_mut(); *guard_mut.0 = 3; - drop(guard); + let key = LockCollection::<(Mutex<_>, Mutex<_>)>::unlock(guard); - let guard = locks.0.lock(&mut key); + let guard = locks.0.lock(key); assert_eq!(guard.as_ref(), &3); - drop(guard); + let key = Mutex::unlock(guard); - let guard = locks.1.lock(&mut key); + let guard = locks.1.lock(key); assert_eq!(guard.as_ref(), &2); - drop(guard); } } |
