From 0c519b6c7801aa6a085551c8e144f0336e615870 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 10 Mar 2024 18:13:20 -0400 Subject: Better librarification --- src/rwlock.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 70 insertions(+), 9 deletions(-) (limited to 'src/rwlock.rs') diff --git a/src/rwlock.rs b/src/rwlock.rs index 722ca2f..f5f0f2b 100644 --- a/src/rwlock.rs +++ b/src/rwlock.rs @@ -1,8 +1,7 @@ -use std::{ - cell::UnsafeCell, - marker::PhantomData, - ops::{Deref, DerefMut}, -}; +use std::cell::UnsafeCell; +use std::fmt::Debug; +use std::marker::PhantomData; +use std::ops::{Deref, DerefMut}; use lock_api::RawRwLock; @@ -17,9 +16,9 @@ pub struct RwLock { value: UnsafeCell, } -pub struct ReadLock<'a, T: ?Sized, R>(pub(crate) &'a RwLock); +pub struct ReadLock<'a, T: ?Sized, R>(&'a RwLock); -pub struct WriteLock<'a, T: ?Sized, R>(pub(crate) &'a RwLock); +pub struct WriteLock<'a, T: ?Sized, R>(&'a RwLock); pub struct RwLockReadRef<'a, T: ?Sized, R: RawRwLock>(&'a RwLock); @@ -37,6 +36,9 @@ pub struct RwLockWriteGuard<'a, 'key, T: ?Sized, Key: Keyable + 'key, R: RawRwLo _phantom: PhantomData<&'key ()>, } +unsafe impl Send for RwLock {} +unsafe impl Sync for RwLock {} + impl<'a, T: ?Sized + 'a, R: RawRwLock> Deref for RwLockReadRef<'a, T, R> { type Target = T; @@ -117,6 +119,7 @@ impl<'a, 'key: 'a, T: ?Sized + 'a, Key: Keyable, R: RawRwLock> { /// Create a guard to the given mutex. Undefined if multiple guards to the /// same mutex exist at once. + #[must_use] const unsafe fn new(rwlock: &'a RwLock, thread_key: Key) -> Self { Self { rwlock: RwLockReadRef(rwlock), @@ -131,6 +134,7 @@ impl<'a, 'key: 'a, T: ?Sized + 'a, Key: Keyable, R: RawRwLock> { /// Create a guard to the given mutex. Undefined if multiple guards to the /// same mutex exist at once. + #[must_use] const unsafe fn new(rwlock: &'a RwLock, thread_key: Key) -> Self { Self { rwlock: RwLockWriteRef(rwlock), @@ -141,6 +145,7 @@ impl<'a, 'key: 'a, T: ?Sized + 'a, Key: Keyable, R: RawRwLock> } impl RwLock { + #[must_use] pub const fn new(value: T) -> Self { Self { value: UnsafeCell::new(value), @@ -149,6 +154,60 @@ impl RwLock { } } +impl Debug for RwLock { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&format!("RwLock<{}>", std::any::type_name::())) + } +} + +impl From for RwLock { + fn from(value: T) -> Self { + Self::new(value) + } +} + +impl AsMut for RwLock { + fn as_mut(&mut self) -> &mut T { + self.get_mut() + } +} + +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> Debug for WriteLock<'a, T, R> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(&format!("WriteLock<{}>", std::any::type_name::())) + } +} + +impl<'a, T: ?Sized, R> From<&'a RwLock> for WriteLock<'a, T, R> { + fn from(value: &'a RwLock) -> Self { + Self::new(value) + } +} + +impl<'a, T: ?Sized, R> AsRef> for WriteLock<'a, T, R> { + fn as_ref(&self) -> &RwLock { + self.0 + } +} + impl RwLock { pub fn into_inner(self) -> T { self.value.into_inner() @@ -273,7 +332,8 @@ impl RwLock { } } -impl<'a, T, R> ReadLock<'a, T, R> { +impl<'a, T: ?Sized, R> ReadLock<'a, T, R> { + #[must_use] pub const fn new(rwlock: &'a RwLock) -> Self { Self(rwlock) } @@ -307,7 +367,8 @@ impl<'a, T: ?Sized, R: RawRwLock> ReadLock<'a, T, R> { } } -impl<'a, T, R> WriteLock<'a, T, R> { +impl<'a, T: ?Sized, R> WriteLock<'a, T, R> { + #[must_use] pub const fn new(rwlock: &'a RwLock) -> Self { Self(rwlock) } -- cgit v1.2.3