diff options
Diffstat (limited to 'src/poisonable')
| -rw-r--r-- | src/poisonable/error.rs | 9 | ||||
| -rw-r--r-- | src/poisonable/guard.rs | 20 | ||||
| -rw-r--r-- | src/poisonable/poisonable.rs | 18 |
3 files changed, 29 insertions, 18 deletions
diff --git a/src/poisonable/error.rs b/src/poisonable/error.rs index 1c4d60a..886b5fd 100644 --- a/src/poisonable/error.rs +++ b/src/poisonable/error.rs @@ -1,5 +1,6 @@ use core::fmt; use std::error::Error; +use std::ops::{Deref, DerefMut}; use super::{PoisonError, PoisonGuard, TryLockPoisonableError}; @@ -66,7 +67,9 @@ impl<Guard> PoisonError<Guard> { pub fn into_inner(self) -> Guard { self.guard } +} +impl<T, Guard: Deref<Target = T>> PoisonError<Guard> { /// Reaches into this error indicating that a lock is poisoned, returning a /// reference to the underlying guard to allow access regardless. /// @@ -96,10 +99,12 @@ impl<Guard> PoisonError<Guard> { /// println!("recovered {} items", data.len()); /// ``` #[must_use] - pub const fn get_ref(&self) -> &Guard { + pub fn get_ref(&self) -> &T { &self.guard } +} +impl<T, Guard: DerefMut<Target = T>> PoisonError<Guard> { /// Reaches into this error indicating that a lock is poisoned, returning a /// mutable reference to the underlying guard to allow access regardless. /// @@ -130,7 +135,7 @@ impl<Guard> PoisonError<Guard> { /// println!("recovered {} items", data.len()); /// ``` #[must_use] - pub fn get_mut(&mut self) -> &mut Guard { + pub fn get_mut(&mut self) -> &mut T { &mut self.guard } } diff --git a/src/poisonable/guard.rs b/src/poisonable/guard.rs index a8a54fe..d1913bf 100644 --- a/src/poisonable/guard.rs +++ b/src/poisonable/guard.rs @@ -68,27 +68,33 @@ impl<'flag, Guard> AsMut<Guard> for PoisonRef<'flag, Guard> { impl<'flag, 'key, Guard: Debug, Key: Keyable> Debug for PoisonGuard<'flag, 'key, Guard, Key> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Debug::fmt(&**self, f) + Debug::fmt(&self.guard, f) } } impl<'flag, 'key, Guard: Display, Key: Keyable> Display for PoisonGuard<'flag, 'key, Guard, Key> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - Display::fmt(&**self, f) + Display::fmt(&self.guard, f) } } -impl<'flag, 'key, Guard, Key: Keyable> Deref for PoisonGuard<'flag, 'key, Guard, Key> { - type Target = Guard; +impl<'flag, 'key, T, Guard: Deref<Target = T>, Key: Keyable> Deref + for PoisonGuard<'flag, 'key, Guard, Key> +{ + type Target = T; fn deref(&self) -> &Self::Target { - &self.guard.guard + #[allow(clippy::explicit_auto_deref)] // fixing this results in a compiler error + &*self.guard.guard } } -impl<'flag, 'key, Guard, Key: Keyable> DerefMut for PoisonGuard<'flag, 'key, Guard, Key> { +impl<'flag, 'key, T, Guard: DerefMut<Target = T>, Key: Keyable> DerefMut + for PoisonGuard<'flag, 'key, Guard, Key> +{ fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.guard.guard + #[allow(clippy::explicit_auto_deref)] // fixing this results in a compiler error + &mut *self.guard.guard } } diff --git a/src/poisonable/poisonable.rs b/src/poisonable/poisonable.rs index cd135ef..ff43ff8 100644 --- a/src/poisonable/poisonable.rs +++ b/src/poisonable/poisonable.rs @@ -103,11 +103,11 @@ impl<L: Lockable + RawLock> Poisonable<L> { /// /// thread::spawn(move || { /// let key = ThreadKey::get().unwrap(); - /// **c_mutex.lock(key).unwrap() = 10; + /// *c_mutex.lock(key).unwrap() = 10; /// }).join().expect("thread::spawn failed"); /// /// let key = ThreadKey::get().unwrap(); - /// assert_eq!(**mutex.lock(key).unwrap(), 10); + /// assert_eq!(*mutex.lock(key).unwrap(), 10); /// ``` pub fn lock<'flag, 'key, Key: Keyable + 'key>( &'flag self, @@ -150,15 +150,15 @@ impl<L: Lockable + RawLock> Poisonable<L> { /// thread::spawn(move || { /// let key = ThreadKey::get().unwrap(); /// let mut lock = c_mutex.try_lock(key); - /// if let Ok(ref mut mutex) = lock { - /// ***mutex = 10; + /// if let Ok(mut mutex) = lock { + /// *mutex = 10; /// } else { /// println!("try_lock failed"); /// } /// }).join().expect("thread::spawn failed"); /// /// let key = ThreadKey::get().unwrap(); - /// assert_eq!(**mutex.lock(key).unwrap(), 10); + /// assert_eq!(*mutex.lock(key).unwrap(), 10); /// ``` /// /// [`Poisoned`]: `TryLockPoisonableError::Poisoned` @@ -187,7 +187,7 @@ impl<L: Lockable + RawLock> Poisonable<L> { /// let mutex = Poisonable::new(Mutex::new(0)); /// /// let mut guard = mutex.lock(key).unwrap(); - /// **guard += 20; + /// *guard += 20; /// /// let key = Poisonable::<Mutex<_>>::unlock(guard); /// ``` @@ -257,13 +257,13 @@ impl<L: Lockable + RawLock> Poisonable<L> { /// /// let key = ThreadKey::get().unwrap(); /// let x = mutex.lock(key).unwrap_or_else(|mut e| { - /// ***e.get_mut() = 1; + /// *e.get_mut() = 1; /// mutex.clear_poison(); /// e.into_inner() /// }); /// /// assert_eq!(mutex.is_poisoned(), false); - /// assert_eq!(**x, 1); + /// assert_eq!(*x, 1); /// ``` pub fn clear_poison(&self) { self.poisoned.clear_poison() @@ -311,7 +311,7 @@ impl<L: Lockable + RawLock> Poisonable<L> { /// let key = ThreadKey::get().unwrap(); /// let mut mutex = Poisonable::new(Mutex::new(0)); /// *mutex.get_mut().unwrap().as_mut() = 10; - /// assert_eq!(**mutex.lock(key).unwrap(), 10); + /// assert_eq!(*mutex.lock(key).unwrap(), 10); /// ``` pub fn get_mut(&mut self) -> PoisonResult<&mut L> { if self.is_poisoned() { |
