summaryrefslogtreecommitdiff
path: root/src/collection/ref.rs
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/ref.rs
parentf5cb25b01f265c9247bd0cb8955addcbaa94fea2 (diff)
Implemented necessary traits
Diffstat (limited to 'src/collection/ref.rs')
-rw-r--r--src/collection/ref.rs33
1 files changed, 18 insertions, 15 deletions
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