summaryrefslogtreecommitdiff
path: root/src/collection
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2024-05-22 15:53:49 -0400
committerBotahamec <botahamec@outlook.com>2024-05-22 15:53:49 -0400
commitebbe3cfce28914d776f3e5f89894fab50911c57e (patch)
tree7ecb7357b9decbe37817eea57e51346aaf055438 /src/collection
parentf5cb25b01f265c9247bd0cb8955addcbaa94fea2 (diff)
Implemented necessary traits
Diffstat (limited to 'src/collection')
-rw-r--r--src/collection/boxed.rs293
-rw-r--r--src/collection/guard.rs25
-rw-r--r--src/collection/owned.rs50
-rw-r--r--src/collection/ref.rs33
-rw-r--r--src/collection/retry.rs102
5 files changed, 459 insertions, 44 deletions
diff --git a/src/collection/boxed.rs b/src/collection/boxed.rs
index 8b67ee9..a62a33d 100644
--- a/src/collection/boxed.rs
+++ b/src/collection/boxed.rs
@@ -1,60 +1,295 @@
-use std::ops::{Deref, DerefMut};
+use std::fmt::Debug;
+use std::marker::PhantomData;
-use crate::{Lockable, OwnedLockable};
+use crate::lockable::Lock;
+use crate::{Keyable, Lockable, OwnedLockable, Sharable};
-use super::{BoxedLockCollection, RefLockCollection};
+use super::{BoxedLockCollection, LockGuard};
-impl<'a, L: 'a> Deref for BoxedLockCollection<'a, L> {
- type Target = RefLockCollection<'a, L>;
+/// returns `true` if the sorted list contains a duplicate
+#[must_use]
+fn contains_duplicates(l: &[&dyn Lock]) -> bool {
+ l.windows(2)
+ .any(|window| std::ptr::eq(window[0], window[1]))
+}
+
+unsafe impl<L: Lockable> Lockable for BoxedLockCollection<L> {
+ type Guard<'g> = L::Guard<'g> where Self: 'g;
+
+ type ReadGuard<'g> = L::ReadGuard<'g> where Self: 'g;
+
+ fn get_ptrs<'a>(&'a self, ptrs: &mut Vec<&'a dyn Lock>) {
+ self.data.get_ptrs(ptrs)
+ }
+
+ unsafe fn guard(&self) -> Self::Guard<'_> {
+ self.data.guard()
+ }
+
+ unsafe fn read_guard(&self) -> Self::ReadGuard<'_> {
+ self.data.read_guard()
+ }
+}
+
+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.data.into_iter()
+ }
+}
+
+impl<'a, L> IntoIterator for &'a BoxedLockCollection<L>
+where
+ &'a L: IntoIterator,
+{
+ type Item = <&'a L as IntoIterator>::Item;
+ type IntoIter = <&'a L as IntoIterator>::IntoIter;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.data.into_iter()
+ }
+}
+
+impl<'a, L> IntoIterator for &'a mut BoxedLockCollection<L>
+where
+ &'a mut L: IntoIterator,
+{
+ type Item = <&'a mut L as IntoIterator>::Item;
+ type IntoIter = <&'a mut L as IntoIterator>::IntoIter;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.data.into_iter()
+ }
+}
+
+impl<L: OwnedLockable, I: FromIterator<L> + OwnedLockable> FromIterator<L>
+ for BoxedLockCollection<I>
+{
+ fn from_iter<T: IntoIterator<Item = L>>(iter: T) -> Self {
+ let iter: I = iter.into_iter().collect();
+ Self::new(iter)
+ }
+}
+
+impl<E: OwnedLockable + Extend<L>, L: OwnedLockable> Extend<L> for BoxedLockCollection<E> {
+ fn extend<T: IntoIterator<Item = L>>(&mut self, iter: T) {
+ self.data.extend(iter)
+ }
+}
+
+impl<L> AsRef<L> for BoxedLockCollection<L> {
+ fn as_ref(&self) -> &L {
+ &self.data
+ }
+}
+
+impl<L> AsMut<L> for BoxedLockCollection<L> {
+ fn as_mut(&mut self) -> &mut L {
+ &mut self.data
+ }
+}
- fn deref(&self) -> &Self::Target {
- &self.0
+impl<L: Debug> Debug for BoxedLockCollection<L> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct(stringify!(BoxedLockCollection))
+ .field("data", &self.data)
+ .finish_non_exhaustive()
}
}
-impl<'a, L: 'a> DerefMut for BoxedLockCollection<'a, L> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.0
+impl<L: OwnedLockable + Default> Default for BoxedLockCollection<L> {
+ fn default() -> Self {
+ Self::new(L::default())
}
}
-impl<'a, L: 'a> Drop for BoxedLockCollection<'a, L> {
- fn drop(&mut self) {
- // this was created with Box::new
- let boxed = unsafe { Box::from_raw((self.0.data as *const L).cast_mut()) };
- drop(boxed);
+impl<L: OwnedLockable + Default> From<L> for BoxedLockCollection<L> {
+ fn from(value: L) -> Self {
+ Self::new(value)
}
}
-impl<'a, L: OwnedLockable + 'a> BoxedLockCollection<'a, L> {
+impl<L: OwnedLockable> BoxedLockCollection<L> {
#[must_use]
pub fn new(data: L) -> Self {
- let boxed = Box::leak(Box::new(data));
- Self(RefLockCollection::new(boxed))
+ // safety: owned lockable types cannot contain duplicates
+ unsafe { Self::new_unchecked(data) }
}
}
-impl<'a, L: OwnedLockable + 'a> BoxedLockCollection<'a, L> {
+impl<'a, L: OwnedLockable> BoxedLockCollection<&'a L> {
#[must_use]
pub fn new_ref(data: &'a L) -> Self {
- let boxed = Box::leak(Box::new(data));
-
- // safety: this is a reference to an OwnedLockable, which can't
- // possibly contain inner duplicates
- Self(unsafe { RefLockCollection::new_unchecked(boxed) })
+ // safety: owned lockable types cannot contain duplicates
+ unsafe { Self::new_unchecked(data) }
}
}
-impl<'a, L: Lockable + 'a> BoxedLockCollection<'a, L> {
+impl<L: Lockable> BoxedLockCollection<L> {
#[must_use]
pub unsafe fn new_unchecked(data: L) -> Self {
- let boxed = Box::leak(Box::new(data));
- Self(RefLockCollection::new_unchecked(boxed))
+ let data = Box::new(data);
+ let mut locks = Vec::new();
+ data.get_ptrs(&mut locks);
+
+ // safety: the box will be dropped after the lock references, so it's
+ // safe to just pretend they're static
+ let locks = std::mem::transmute(locks);
+ Self { data, locks }
}
#[must_use]
pub fn try_new(data: L) -> Option<Self> {
- let boxed = Box::leak(Box::new(data));
- RefLockCollection::try_new(boxed).map(Self)
+ // safety: we are checking for duplicates before returning
+ unsafe {
+ let this = Self::new_unchecked(data);
+ if contains_duplicates(&this.locks) {
+ return None;
+ }
+ Some(this)
+ }
+ }
+
+ #[must_use]
+ pub fn into_inner(self) -> Box<L> {
+ self.data
+ }
+
+ pub fn lock<'g, 'key: 'g, Key: Keyable + 'key>(
+ &'g self,
+ key: Key,
+ ) -> LockGuard<'key, L::Guard<'g>, Key> {
+ for lock in &self.locks {
+ // safety: we have the thread key
+ unsafe { lock.lock() };
+ }
+
+ LockGuard {
+ // safety: we've already acquired the lock
+ guard: unsafe { self.data.guard() },
+ key,
+ _phantom: PhantomData,
+ }
+ }
+
+ pub fn try_lock<'g, 'key: 'g, Key: Keyable + 'key>(
+ &'g self,
+ key: Key,
+ ) -> Option<LockGuard<'key, L::Guard<'g>, Key>> {
+ let guard = unsafe {
+ for (i, lock) in self.locks.iter().enumerate() {
+ // safety: we have the thread key
+ let success = lock.try_lock();
+
+ if !success {
+ for lock in &self.locks[0..i] {
+ // safety: this lock was already acquired
+ lock.unlock();
+ }
+ return None;
+ }
+ }
+
+ // safety: we've acquired the locks
+ self.data.guard()
+ };
+
+ Some(LockGuard {
+ guard,
+ key,
+ _phantom: PhantomData,
+ })
+ }
+
+ pub fn unlock<'key, Key: Keyable + 'key>(guard: LockGuard<'key, L::Guard<'_>, Key>) -> Key {
+ drop(guard.guard);
+ guard.key
+ }
+}
+
+impl<L: Sharable> BoxedLockCollection<L> {
+ pub fn read<'g, 'key: 'g, Key: Keyable + 'key>(
+ &'g self,
+ key: Key,
+ ) -> LockGuard<'key, L::ReadGuard<'g>, Key> {
+ for lock in &self.locks {
+ // safety: we have the thread key
+ unsafe { lock.read() };
+ }
+
+ LockGuard {
+ // safety: we've already acquired the lock
+ guard: unsafe { self.data.read_guard() },
+ key,
+ _phantom: PhantomData,
+ }
+ }
+
+ pub fn try_read<'g, 'key: 'g, Key: Keyable + 'key>(
+ &'g self,
+ key: Key,
+ ) -> Option<LockGuard<'key, L::ReadGuard<'g>, Key>> {
+ let guard = unsafe {
+ for (i, lock) in self.locks.iter().enumerate() {
+ // safety: we have the thread key
+ let success = lock.try_read();
+
+ if !success {
+ for lock in &self.locks[0..i] {
+ // safety: this lock was already acquired
+ lock.unlock_read();
+ }
+ return None;
+ }
+ }
+
+ // safety: we've acquired the locks
+ self.data.read_guard()
+ };
+
+ Some(LockGuard {
+ guard,
+ key,
+ _phantom: PhantomData,
+ })
+ }
+
+ pub fn unlock_read<'key, Key: Keyable + 'key>(
+ guard: LockGuard<'key, L::ReadGuard<'_>, Key>,
+ ) -> Key {
+ drop(guard.guard);
+ guard.key
+ }
+}
+
+impl<'a, L: 'a> BoxedLockCollection<L>
+where
+ &'a L: IntoIterator,
+{
+ /// Returns an iterator over references to each value in the collection.
+ #[must_use]
+ pub fn iter(&'a self) -> <&'a L as IntoIterator>::IntoIter {
+ self.into_iter()
+ }
+}
+
+impl<'a, L: 'a> BoxedLockCollection<L>
+where
+ &'a mut L: IntoIterator,
+{
+ /// Returns an iterator over mutable references to each value in the
+ /// collection.
+ #[must_use]
+ pub fn iter_mut(&'a mut self) -> <&'a mut L as IntoIterator>::IntoIter {
+ self.into_iter()
}
}
diff --git a/src/collection/guard.rs b/src/collection/guard.rs
index b8561eb..8857c5f 100644
--- a/src/collection/guard.rs
+++ b/src/collection/guard.rs
@@ -1,9 +1,22 @@
+use std::fmt::{Debug, Display};
use std::ops::{Deref, DerefMut};
use crate::key::Keyable;
use super::LockGuard;
+impl<'key, Guard: Debug, Key: Keyable> Debug for LockGuard<'key, Guard, Key> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ Debug::fmt(&**self, f)
+ }
+}
+
+impl<'key, Guard: Display, Key: Keyable> Display for LockGuard<'key, Guard, Key> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ Display::fmt(&**self, f)
+ }
+}
+
impl<'key, Guard, Key: Keyable> Deref for LockGuard<'key, Guard, Key> {
type Target = Guard;
@@ -17,3 +30,15 @@ impl<'key, Guard, Key: Keyable> DerefMut for LockGuard<'key, Guard, Key> {
&mut self.guard
}
}
+
+impl<'key, Guard, Key: Keyable> AsRef<Guard> for LockGuard<'key, Guard, Key> {
+ fn as_ref(&self) -> &Guard {
+ &self.guard
+ }
+}
+
+impl<'key, Guard, Key: Keyable> AsMut<Guard> for LockGuard<'key, Guard, Key> {
+ fn as_mut(&mut self) -> &mut Guard {
+ &mut self.guard
+ }
+}
diff --git a/src/collection/owned.rs b/src/collection/owned.rs
index 3415ac4..eb5e03a 100644
--- a/src/collection/owned.rs
+++ b/src/collection/owned.rs
@@ -32,12 +32,62 @@ unsafe impl<L: Sharable> Sharable for OwnedLockCollection<L> {}
unsafe impl<L: OwnedLockable> OwnedLockable for OwnedLockCollection<L> {}
+impl<L> IntoIterator for OwnedLockCollection<L>
+where
+ L: IntoIterator,
+{
+ type Item = <L as IntoIterator>::Item;
+ type IntoIter = <L as IntoIterator>::IntoIter;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.data.into_iter()
+ }
+}
+
+impl<L: OwnedLockable, I: FromIterator<L> + OwnedLockable> FromIterator<L>
+ for OwnedLockCollection<I>
+{
+ fn from_iter<T: IntoIterator<Item = L>>(iter: T) -> Self {
+ let iter: I = iter.into_iter().collect();
+ Self::new(iter)
+ }
+}
+
+impl<E: OwnedLockable + Extend<L>, L: OwnedLockable> Extend<L> for OwnedLockCollection<E> {
+ fn extend<T: IntoIterator<Item = L>>(&mut self, iter: T) {
+ self.data.extend(iter)
+ }
+}
+
+impl<L: OwnedLockable> AsMut<L> for OwnedLockCollection<L> {
+ fn as_mut(&mut self) -> &mut L {
+ &mut self.data
+ }
+}
+
+impl<L: OwnedLockable + Default> Default for OwnedLockCollection<L> {
+ fn default() -> Self {
+ Self::new(L::default())
+ }
+}
+
+impl<L: OwnedLockable + Default> From<L> for OwnedLockCollection<L> {
+ fn from(value: L) -> Self {
+ Self::new(value)
+ }
+}
+
impl<L: OwnedLockable> OwnedLockCollection<L> {
#[must_use]
pub const fn new(data: L) -> Self {
Self { data }
}
+ #[must_use]
+ pub fn into_inner(self) -> L {
+ self.data
+ }
+
pub fn lock<'g, 'key, Key: Keyable + 'key>(
&'g self,
key: Key,
diff --git a/src/collection/ref.rs b/src/collection/ref.rs
index da18c62..329f0ae 100644
--- a/src/collection/ref.rs
+++ b/src/collection/ref.rs
@@ -1,3 +1,4 @@
+use std::fmt::Debug;
use std::marker::PhantomData;
use crate::{key::Keyable, lockable::Lock, Lockable, OwnedLockable, Sharable};
@@ -5,7 +6,7 @@ use crate::{key::Keyable, lockable::Lock, Lockable, OwnedLockable, Sharable};
use super::{LockGuard, RefLockCollection};
#[must_use]
-fn get_locks<L: Lockable>(data: &L) -> Vec<&dyn Lock> {
+pub fn get_locks<L: Lockable>(data: &L) -> Vec<&dyn Lock> {
let mut locks = Vec::new();
data.get_ptrs(&mut locks);
locks.sort_by_key(|lock| std::ptr::from_ref(*lock));
@@ -19,24 +20,12 @@ fn contains_duplicates(l: &[&dyn Lock]) -> bool {
.any(|window| std::ptr::eq(window[0], window[1]))
}
-impl<'a, L: Lockable> AsRef<L> for RefLockCollection<'a, L> {
+impl<'a, L> AsRef<L> for RefLockCollection<'a, L> {
fn as_ref(&self) -> &L {
self.data
}
}
-impl<'a, L: Lockable> AsRef<Self> for RefLockCollection<'a, L> {
- fn as_ref(&self) -> &Self {
- self
- }
-}
-
-impl<'a, L: Lockable> AsMut<Self> for RefLockCollection<'a, L> {
- fn as_mut(&mut self) -> &mut Self {
- self
- }
-}
-
impl<'a, L> IntoIterator for &'a RefLockCollection<'a, L>
where
&'a L: IntoIterator,
@@ -69,6 +58,20 @@ unsafe impl<'c, L: Lockable> Lockable for RefLockCollection<'c, L> {
unsafe impl<'c, L: Sharable> Sharable for RefLockCollection<'c, L> {}
+impl<'a, L: Debug> Debug for RefLockCollection<'a, L> {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct(stringify!(RefLockCollection))
+ .field("data", self.data)
+ .finish_non_exhaustive()
+ }
+}
+
+impl<'a, L: OwnedLockable + Default> From<&'a L> for RefLockCollection<'a, L> {
+ fn from(value: &'a L) -> Self {
+ Self::new(value)
+ }
+}
+
impl<'a, L: OwnedLockable> RefLockCollection<'a, L> {
/// Creates a new collection of owned locks.
///
@@ -142,7 +145,7 @@ impl<'a, L: Lockable> RefLockCollection<'a, L> {
return None;
}
- Some(Self { locks, data })
+ Some(Self { data, locks })
}
/// Locks the collection
diff --git a/src/collection/retry.rs b/src/collection/retry.rs
index 3000f8b..58a0642 100644
--- a/src/collection/retry.rs
+++ b/src/collection/retry.rs
@@ -41,6 +41,81 @@ unsafe impl<L: Sharable> Sharable for RetryingLockCollection<L> {}
unsafe impl<L: OwnedLockable> OwnedLockable for RetryingLockCollection<L> {}
+impl<L> IntoIterator for RetryingLockCollection<L>
+where
+ L: IntoIterator,
+{
+ type Item = <L as IntoIterator>::Item;
+ type IntoIter = <L as IntoIterator>::IntoIter;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.data.into_iter()
+ }
+}
+
+impl<'a, L> IntoIterator for &'a RetryingLockCollection<L>
+where
+ &'a L: IntoIterator,
+{
+ type Item = <&'a L as IntoIterator>::Item;
+ type IntoIter = <&'a L as IntoIterator>::IntoIter;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.data.into_iter()
+ }
+}
+
+impl<'a, L> IntoIterator for &'a mut RetryingLockCollection<L>
+where
+ &'a mut L: IntoIterator,
+{
+ type Item = <&'a mut L as IntoIterator>::Item;
+ type IntoIter = <&'a mut L as IntoIterator>::IntoIter;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.data.into_iter()
+ }
+}
+
+impl<L: OwnedLockable, I: FromIterator<L> + OwnedLockable> FromIterator<L>
+ for RetryingLockCollection<I>
+{
+ fn from_iter<T: IntoIterator<Item = L>>(iter: T) -> Self {
+ let iter: I = iter.into_iter().collect();
+ Self::new(iter)
+ }
+}
+
+impl<E: OwnedLockable + Extend<L>, L: OwnedLockable> Extend<L> for RetryingLockCollection<E> {
+ fn extend<T: IntoIterator<Item = L>>(&mut self, iter: T) {
+ self.data.extend(iter)
+ }
+}
+
+impl<L> AsRef<L> for RetryingLockCollection<L> {
+ fn as_ref(&self) -> &L {
+ &self.data
+ }
+}
+
+impl<L> AsMut<L> for RetryingLockCollection<L> {
+ fn as_mut(&mut self) -> &mut L {
+ &mut self.data
+ }
+}
+
+impl<L: OwnedLockable + Default> Default for RetryingLockCollection<L> {
+ fn default() -> Self {
+ Self::new(L::default())
+ }
+}
+
+impl<L: OwnedLockable> From<L> for RetryingLockCollection<L> {
+ fn from(value: L) -> Self {
+ Self::new(value)
+ }
+}
+
impl<L: OwnedLockable> RetryingLockCollection<L> {
#[must_use]
pub const fn new(data: L) -> Self {
@@ -65,6 +140,10 @@ impl<L: Lockable> RetryingLockCollection<L> {
contains_duplicates(&data).then_some(Self { data })
}
+ pub fn into_inner(self) -> L {
+ self.data
+ }
+
pub fn lock<'g, 'key: 'g, Key: Keyable + 'key>(
&'g self,
key: Key,
@@ -269,3 +348,26 @@ impl<L: Sharable> RetryingLockCollection<L> {
guard.key
}
}
+
+impl<'a, L: 'a> RetryingLockCollection<L>
+where
+ &'a L: IntoIterator,
+{
+ /// Returns an iterator over references to each value in the collection.
+ #[must_use]
+ pub fn iter(&'a self) -> <&'a L as IntoIterator>::IntoIter {
+ self.into_iter()
+ }
+}
+
+impl<'a, L: 'a> RetryingLockCollection<L>
+where
+ &'a mut L: IntoIterator,
+{
+ /// Returns an iterator over mutable references to each value in the
+ /// collection.
+ #[must_use]
+ pub fn iter_mut(&'a mut self) -> <&'a mut L as IntoIterator>::IntoIter {
+ self.into_iter()
+ }
+}