use std::ops::{Deref, DerefMut}; use parking_lot::{ MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard, }; #[derive(Debug)] pub struct Lock { inner: RwLock, } impl Lock { pub fn new(value: Value) -> Self { Self { inner: RwLock::new(value) } } /// Tries to a acquire a handle to the resource with read access. /// /// # Errors /// Returns `Err` if unavailable (A mutable handle is hold). pub fn read_nonblock(&self) -> Result, Error> { let guard = self.inner.try_read().ok_or(Error::ReadUnavailable)?; Ok(ReadGuard { inner: guard }) } /// Tries to a acquire a handle to the resource with mutable access. /// /// # Errors /// Returns `Err` if unavailable (A mutable or immutable handle is hold). pub fn write_nonblock(&self) -> Result, Error> { let guard = self.inner.try_write().ok_or(Error::WriteUnavailable)?; Ok(WriteGuard { inner: guard }) } } impl Default for Lock { fn default() -> Self { Self::new(Value::default()) } } #[derive(Debug, thiserror::Error)] pub enum Error { #[error("Lock is unavailable for reading")] ReadUnavailable, #[error("Lock is unavailable for writing")] WriteUnavailable, } #[derive(Debug)] pub struct ReadGuard<'guard, Value: ?Sized> { inner: RwLockReadGuard<'guard, Value>, } impl<'guard, Value: ?Sized> ReadGuard<'guard, Value> { pub fn map( this: Self, func: impl FnOnce(&Value) -> &NewValue, ) -> MappedReadGuard<'guard, NewValue> { MappedReadGuard { inner: RwLockReadGuard::map(this.inner, func), } } pub fn try_map( this: Self, func: impl FnOnce(&Value) -> Option<&NewValue>, ) -> Result, Self> { match RwLockReadGuard::try_map(this.inner, func) { Ok(mapped_guard) => Ok(MappedReadGuard { inner: mapped_guard }), Err(unmapped_guard) => Err(Self { inner: unmapped_guard }), } } } impl Deref for ReadGuard<'_, Value> { type Target = Value; fn deref(&self) -> &Self::Target { &self.inner } } #[derive(Debug)] pub struct MappedReadGuard<'guard, Value: ?Sized> { inner: MappedRwLockReadGuard<'guard, Value>, } impl Deref for MappedReadGuard<'_, Value> { type Target = Value; fn deref(&self) -> &Self::Target { &self.inner } } #[derive(Debug)] pub struct WriteGuard<'guard, Value: ?Sized> { inner: RwLockWriteGuard<'guard, Value>, } impl<'guard, Value: ?Sized> WriteGuard<'guard, Value> { pub fn map( this: Self, func: impl FnOnce(&mut Value) -> &mut NewValue, ) -> MappedWriteGuard<'guard, NewValue> { MappedWriteGuard { inner: RwLockWriteGuard::map(this.inner, func), } } pub fn try_map( this: Self, func: impl FnOnce(&mut Value) -> Option<&mut NewValue>, ) -> Result, Self> { match RwLockWriteGuard::try_map(this.inner, func) { Ok(mapped_guard) => Ok(MappedWriteGuard { inner: mapped_guard }), Err(unmapped_guard) => Err(Self { inner: unmapped_guard }), } } } impl Deref for WriteGuard<'_, Value> { type Target = Value; fn deref(&self) -> &Self::Target { &self.inner } } impl DerefMut for WriteGuard<'_, Value> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } } #[derive(Debug)] pub struct MappedWriteGuard<'guard, Value: ?Sized> { inner: MappedRwLockWriteGuard<'guard, Value>, } impl Deref for MappedWriteGuard<'_, Value> { type Target = Value; fn deref(&self) -> &Self::Target { &self.inner } } impl DerefMut for MappedWriteGuard<'_, Value> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.inner } }