From fe67aa262f1b04fb6c38683d9221c3a2fafcc35a Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 10 Mar 2024 20:21:00 -0400 Subject: Reorganization --- src/rwlock/read_lock.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/rwlock/read_lock.rs (limited to 'src/rwlock/read_lock.rs') diff --git a/src/rwlock/read_lock.rs b/src/rwlock/read_lock.rs new file mode 100644 index 0000000..dbab8de --- /dev/null +++ b/src/rwlock/read_lock.rs @@ -0,0 +1,60 @@ +use std::fmt::Debug; + +use lock_api::RawRwLock; + +use crate::key::Keyable; + +use super::{ReadLock, RwLock, RwLockReadGuard, RwLockReadRef}; + +impl<'a, T: ?Sized, R> Debug for ReadLock<'a, T, R> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&format!("ReadLock<{}>", std::any::type_name::())) + } +} + +impl<'a, T: ?Sized, R> From<&'a RwLock> for ReadLock<'a, T, R> { + fn from(value: &'a RwLock) -> Self { + Self::new(value) + } +} + +impl<'a, T: ?Sized, R> AsRef> for ReadLock<'a, T, R> { + fn as_ref(&self) -> &RwLock { + self.0 + } +} + +impl<'a, T: ?Sized, R> ReadLock<'a, T, R> { + #[must_use] + pub const fn new(rwlock: &'a RwLock) -> Self { + Self(rwlock) + } +} + +impl<'a, T: ?Sized, R: RawRwLock> ReadLock<'a, T, R> { + pub fn lock<'s, 'key: 's, Key: Keyable + 'key>( + &'s self, + key: Key, + ) -> RwLockReadGuard<'_, 'key, T, Key, R> { + self.0.read(key) + } + + pub(crate) unsafe fn lock_no_key(&self) -> RwLockReadRef<'_, T, R> { + self.0.read_no_key() + } + + pub fn try_lock<'s, 'key: 's, Key: Keyable + 'key>( + &'s self, + key: Key, + ) -> Option> { + self.0.try_read(key) + } + + pub(crate) unsafe fn try_lock_no_key(&self) -> Option> { + self.0.try_read_no_key() + } + + pub fn unlock<'key, Key: Keyable + 'key>(guard: RwLockReadGuard<'_, 'key, T, Key, R>) -> Key { + RwLock::unlock_read(guard) + } +} -- cgit v1.2.3