summaryrefslogtreecommitdiff
path: root/src/collection/guard.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2025-02-05 20:31:00 -0500
committerBotahamec <botahamec@outlook.com>2025-02-05 20:31:00 -0500
commitf6b38f7425a3183214dae79445446b042154688f (patch)
tree1219d7cc4420ff4ad58a017c0f5861b7a2936f3b /src/collection/guard.rs
parent280a61ad7b74019c7aad8b7306a0dd7cfb11359c (diff)
Tests and optimization
Diffstat (limited to 'src/collection/guard.rs')
-rw-r--r--src/collection/guard.rs63
1 files changed, 62 insertions, 1 deletions
diff --git a/src/collection/guard.rs b/src/collection/guard.rs
index 9412343..eea13ed 100644
--- a/src/collection/guard.rs
+++ b/src/collection/guard.rs
@@ -7,6 +7,7 @@ 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)
@@ -14,6 +15,7 @@ impl<Guard: PartialEq, Key: Keyable> PartialEq for LockGuard<'_, Guard, Key> {
}
#[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)
@@ -21,9 +23,11 @@ impl<Guard: PartialOrd, Key: Keyable> PartialOrd for LockGuard<'_, Guard, Key> {
}
#[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)
@@ -31,6 +35,7 @@ impl<Guard: Ord, Key: Keyable> Ord for LockGuard<'_, Guard, Key> {
}
#[mutants::skip] // hashing involves RNG and is hard to test
+#[cfg(not(tarpaulin_include))]
impl<Guard: Hash, Key: Keyable> Hash for LockGuard<'_, Guard, Key> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.guard.hash(state)
@@ -38,6 +43,7 @@ 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> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&**self, f)
@@ -79,7 +85,7 @@ impl<Guard, Key: Keyable> AsMut<Guard> for LockGuard<'_, Guard, Key> {
#[cfg(test)]
mod tests {
use crate::collection::OwnedLockCollection;
- use crate::{RwLock, ThreadKey};
+ use crate::{LockCollection, Mutex, RwLock, ThreadKey};
#[test]
fn guard_display_works() {
@@ -88,4 +94,59 @@ mod tests {
let guard = lock.read(key);
assert_eq!(guard.to_string(), "Hello, world!".to_string());
}
+
+ #[test]
+ fn deref_mut_works() {
+ let mut 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);
+ *guard.0 = 3;
+ drop(guard);
+
+ let guard = locks.0.lock(&mut key);
+ assert_eq!(*guard, 3);
+ drop(guard);
+
+ let guard = locks.1.lock(&mut key);
+ assert_eq!(*guard, 2);
+ drop(guard);
+ }
+
+ #[test]
+ fn as_ref_works() {
+ let mut 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);
+ *guard.0 = 3;
+ drop(guard);
+
+ let guard = locks.0.lock(&mut key);
+ assert_eq!(guard.as_ref(), &3);
+ drop(guard);
+
+ let guard = locks.1.lock(&mut key);
+ assert_eq!(guard.as_ref(), &2);
+ drop(guard);
+ }
+
+ #[test]
+ fn as_mut_works() {
+ let mut 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 guard_mut = guard.as_mut();
+ *guard_mut.0 = 3;
+ drop(guard);
+
+ let guard = locks.0.lock(&mut key);
+ assert_eq!(guard.as_ref(), &3);
+ drop(guard);
+
+ let guard = locks.1.lock(&mut key);
+ assert_eq!(guard.as_ref(), &2);
+ drop(guard);
+ }
}