From 4a5ec04a29cba07c5960792528bd66b0f99ee3ee Mon Sep 17 00:00:00 2001 From: Botahamec Date: Fri, 7 Feb 2025 17:48:26 -0500 Subject: Fix lifetimes for poison guards --- src/collection/boxed.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'src/collection/boxed.rs') diff --git a/src/collection/boxed.rs b/src/collection/boxed.rs index 72489bf..0597e90 100644 --- a/src/collection/boxed.rs +++ b/src/collection/boxed.rs @@ -627,6 +627,69 @@ mod tests { use super::*; use crate::{Mutex, RwLock, ThreadKey}; + #[test] + fn from_iterator() { + let key = ThreadKey::get().unwrap(); + let collection: BoxedLockCollection>> = + [Mutex::new("foo"), Mutex::new("bar"), Mutex::new("baz")] + .into_iter() + .collect(); + let guard = collection.lock(key); + // TODO impl PartialEq for MutexRef + assert_eq!(*guard[0], "foo"); + assert_eq!(*guard[1], "bar"); + assert_eq!(*guard[2], "baz"); + } + + #[test] + fn from() { + let key = ThreadKey::get().unwrap(); + let collection = + BoxedLockCollection::from([Mutex::new("foo"), Mutex::new("bar"), Mutex::new("baz")]); + let guard = collection.lock(key); + // TODO impl PartialEq for MutexRef + assert_eq!(*guard[0], "foo"); + assert_eq!(*guard[1], "bar"); + assert_eq!(*guard[2], "baz"); + } + + #[test] + fn into_owned_iterator() { + let collection = BoxedLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]); + for (i, mutex) in collection.into_iter().enumerate() { + assert_eq!(mutex.into_inner(), i); + } + } + + #[test] + fn into_ref_iterator() { + let mut key = ThreadKey::get().unwrap(); + let collection = BoxedLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]); + for (i, mutex) in (&collection).into_iter().enumerate() { + assert_eq!(*mutex.lock(&mut key), i); + } + } + + #[test] + fn ref_iterator() { + let mut key = ThreadKey::get().unwrap(); + let collection = BoxedLockCollection::new([Mutex::new(0), Mutex::new(1), Mutex::new(2)]); + for (i, mutex) in collection.iter().enumerate() { + assert_eq!(*mutex.lock(&mut key), i); + } + } + + #[test] + #[allow(clippy::float_cmp)] + fn uses_correct_default() { + let collection = + BoxedLockCollection::<(Mutex, Mutex>, Mutex)>::default(); + let tuple = collection.into_inner(); + assert_eq!(tuple.0, 0.0); + assert!(tuple.1.is_none()); + assert_eq!(tuple.2, 0) + } + #[test] fn non_duplicates_allowed() { let mutex1 = Mutex::new(0); @@ -732,6 +795,36 @@ mod tests { assert!(guard.is_ok()); } + #[test] + fn unlock_collection_works() { + let key = ThreadKey::get().unwrap(); + let mutex1 = Mutex::new("foo"); + let mutex2 = Mutex::new("bar"); + let collection = BoxedLockCollection::try_new((&mutex1, &mutex2)).unwrap(); + let guard = collection.lock(key); + let key = BoxedLockCollection::<(&Mutex<_>, &Mutex<_>)>::unlock(guard); + + assert!(mutex1.try_lock(key).is_ok()) + } + + #[test] + fn read_unlock_collection_works() { + let key = ThreadKey::get().unwrap(); + let lock1 = RwLock::new("foo"); + let lock2 = RwLock::new("bar"); + let collection = BoxedLockCollection::try_new((&lock1, &lock2)).unwrap(); + let guard = collection.read(key); + let key = BoxedLockCollection::<(&RwLock<_>, &RwLock<_>)>::unlock_read(guard); + + assert!(lock1.try_write(key).is_ok()) + } + + #[test] + fn into_inner_works() { + let collection = BoxedLockCollection::new((Mutex::new("Hello"), Mutex::new(47))); + assert_eq!(collection.into_inner(), ("Hello", 47)) + } + #[test] fn works_in_collection() { let key = ThreadKey::get().unwrap(); -- cgit v1.2.3