From cd4a2f87ec186474c2f944488f42274855a15ecb Mon Sep 17 00:00:00 2001 From: Micha White Date: Fri, 31 May 2024 11:00:31 -0400 Subject: Add into_inner --- src/collection/boxed.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/collection/boxed.rs b/src/collection/boxed.rs index f12a97a..8e3076e 100644 --- a/src/collection/boxed.rs +++ b/src/collection/boxed.rs @@ -38,6 +38,18 @@ unsafe impl Sharable for BoxedLockCollection {} unsafe impl OwnedLockable for BoxedLockCollection {} +impl IntoIterator for BoxedLockCollection +where + L: IntoIterator, +{ + type Item = ::Item; + type IntoIter = ::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.into_inner().into_iter() + } +} + impl<'a, L> IntoIterator for &'a BoxedLockCollection where &'a L: IntoIterator, @@ -98,6 +110,30 @@ impl From for BoxedLockCollection { } impl BoxedLockCollection { + /// Gets the underlying collection, consuming this collection. + /// + /// # Examples + /// + /// ``` + /// use happylock::{Mutex, ThreadKey, LockCollection}; + /// + /// let data1 = Mutex::new(42); + /// let data2 = Mutex::new(""); + /// + /// // data1 and data2 refer to distinct mutexes, so this won't panic + /// let data = (&data1, &data2); + /// let lock = LockCollection::try_new(&data).unwrap(); + /// + /// let key = ThreadKey::get().unwrap(); + /// let guard = lock.into_inner().0.lock(key); + /// assert_eq!(*guard, 42); + /// ``` + #[must_use] + pub fn into_inner(self) -> L { + // safety: this is owned, so no other references exist + unsafe { self.data.read().into_inner() } + } + /// Gets an immutable reference to the underlying data fn data(&self) -> &L { unsafe { -- cgit v1.2.3