summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2024-05-31 11:00:31 -0400
committerMicha White <botahamec@outlook.com>2024-05-31 11:00:31 -0400
commitcd4a2f87ec186474c2f944488f42274855a15ecb (patch)
tree770e2e79c8d2b529cae2cf49a464f9725bcb12c1
parent2c26dd547323d39efb7aa6bf9fdf081b8953c223 (diff)
Add into_inner
-rw-r--r--src/collection/boxed.rs36
1 files changed, 36 insertions, 0 deletions
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<L: Sharable> Sharable for BoxedLockCollection<L> {}
unsafe impl<L: OwnedLockable> OwnedLockable for BoxedLockCollection<L> {}
+impl<L> IntoIterator for BoxedLockCollection<L>
+where
+ L: IntoIterator,
+{
+ type Item = <L as IntoIterator>::Item;
+ type IntoIter = <L as IntoIterator>::IntoIter;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.into_inner().into_iter()
+ }
+}
+
impl<'a, L> IntoIterator for &'a BoxedLockCollection<L>
where
&'a L: IntoIterator,
@@ -98,6 +110,30 @@ impl<L: OwnedLockable + Default> From<L> for BoxedLockCollection<L> {
}
impl<L> BoxedLockCollection<L> {
+ /// 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 {