diff options
| author | Mica White <botahamec@outlook.com> | 2026-08-26 20:46:31 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-08-26 20:46:31 -0400 |
| commit | 55b3a2425b242fbc5c6e471f220eeb8b949e8751 (patch) | |
| tree | 5ceb7910b60cc7331024b7ce1d49ec259e0302a8 /src/collection | |
| parent | 6f6e030ea7edb9d155ebf21b5d42936c20801b50 (diff) | |
Add tests
Diffstat (limited to 'src/collection')
| -rwxr-xr-x | src/collection/boxed.rs | 2 | ||||
| -rwxr-xr-x | src/collection/owned.rs | 40 |
2 files changed, 36 insertions, 6 deletions
diff --git a/src/collection/boxed.rs b/src/collection/boxed.rs index 1478120..66e6df0 100755 --- a/src/collection/boxed.rs +++ b/src/collection/boxed.rs @@ -721,6 +721,8 @@ impl<L: Sharable> BoxedLockCollection<L> { } } +impl<L: OwnedLockable> BoxedLockCollection<L> {} + impl<L: LockableIntoInner> BoxedLockCollection<L> { /// Consumes this `BoxedLockCollection`, returning the underlying data. /// diff --git a/src/collection/owned.rs b/src/collection/owned.rs index 07a5402..483bc09 100755 --- a/src/collection/owned.rs +++ b/src/collection/owned.rs @@ -1,4 +1,4 @@ -use crate::iterator::LockContext; +use crate::context::LockContext; use crate::lockable::{ Lockable, LockableGetMut, LockableIntoInner, OwnedLockable, RawLock, Sharable, }; @@ -61,7 +61,7 @@ unsafe impl<L: Lockable> Lockable for OwnedLockCollection<L> { where Self: 'a; - #[mutants::skip] // It's hard to test lkocks in an OwnedLockCollection, because they're owned + #[mutants::skip] // It's hard to test locks in an OwnedLockCollection, because they're owned #[cfg(not(tarpaulin_include))] fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn RawLock>) { // It's ok to use self here, because the values in the collection already @@ -350,6 +350,38 @@ impl<L: OwnedLockable> OwnedLockCollection<L> { Ok(LockGuard { guard, key }) } + /// Creates a context that can be used to iterate over the items in order. + /// + /// Sometimes, partial allocation of locks is useful. For example, you may + /// want to acquire a lock on the first element of a list before deciding if + /// the second element should be locked. This function creates a + /// [`LockContext`] which is capable of doing exactly that. + /// + /// # Example + /// + /// ``` + /// use happylock::{Mutex, ThreadKey}; + /// use happylock::collection::OwnedLockCollection; + /// + /// let key = ThreadKey::get().unwrap(); + /// let data = (Mutex::new(true), Mutex::new(42), Mutex::new(67)); + /// let locks = OwnedLockCollection::new(data); + /// let mut ctx = locks.context(); + /// let tuple = ctx.tuple(key); + /// + /// let (use_other, tuple) = tuple.lock_0(); + /// let number = if **use_other { + /// tuple.lock_2().0 + /// } else { + /// tuple.lock_1().0 + /// }; + /// assert_eq!(**number, 67); + /// ``` + #[must_use] + pub const fn context(&self) -> LockContext<'_, L> { + LockContext::new(&self.child) + } + /// Unlocks the underlying lockable data type, returning the key that's /// associated with it. /// @@ -559,10 +591,6 @@ impl<L: Sharable> OwnedLockCollection<L> { } impl<L> OwnedLockCollection<L> { - pub const fn context(&self) -> LockContext<'_, L> { - LockContext::new(&self.child) - } - /// Gets the underlying collection, consuming this collection. /// /// # Examples |
