From 311842d28d1fbb6da945f0d98f1655f77a58abf9 Mon Sep 17 00:00:00 2001 From: Mica White Date: Wed, 25 Dec 2024 17:58:38 -0500 Subject: as_mut re-organization --- src/poisonable/poisonable.rs | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'src/poisonable') diff --git a/src/poisonable/poisonable.rs b/src/poisonable/poisonable.rs index 57436eb..79f90d9 100644 --- a/src/poisonable/poisonable.rs +++ b/src/poisonable/poisonable.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; use std::panic::{RefUnwindSafe, UnwindSafe}; use crate::lockable::{ - Lockable, LockableAsMut, LockableIntoInner, OwnedLockable, RawLock, Sharable, + Lockable, LockableGetMut, LockableIntoInner, OwnedLockable, RawLock, Sharable, }; use crate::Keyable; @@ -81,6 +81,33 @@ unsafe impl Sharable for Poisonable { unsafe impl OwnedLockable for Poisonable {} +impl LockableGetMut for Poisonable { + type Inner<'a> + = PoisonResult> + where + Self: 'a; + + fn get_mut(&mut self) -> Self::Inner<'_> { + if self.is_poisoned() { + Err(PoisonError::new(self.inner.get_mut())) + } else { + Ok(self.inner.get_mut()) + } + } +} + +impl LockableIntoInner for Poisonable { + type Inner = PoisonResult; + + fn into_inner(self) -> Self::Inner { + if self.is_poisoned() { + Err(PoisonError::new(self.inner.into_inner())) + } else { + Ok(self.inner.into_inner()) + } + } +} + impl From for Poisonable { fn from(value: L) -> Self { Self::new(value) @@ -524,15 +551,11 @@ impl Poisonable { /// assert_eq!(mutex.into_inner().unwrap(), 0); /// ``` pub fn into_inner(self) -> PoisonResult { - if self.is_poisoned() { - Err(PoisonError::new(self.inner.into_inner())) - } else { - Ok(self.inner.into_inner()) - } + LockableIntoInner::into_inner(self) } } -impl Poisonable { +impl Poisonable { /// Returns a mutable reference to the underlying data. /// /// Since this call borrows the `Poisonable` mutable, no actual locking @@ -555,11 +578,7 @@ impl Poisonable { /// assert_eq!(*mutex.lock(key).unwrap(), 10); /// ``` pub fn get_mut(&mut self) -> PoisonResult> { - if self.is_poisoned() { - Err(PoisonError::new(self.inner.as_mut())) - } else { - Ok(self.inner.as_mut()) - } + LockableGetMut::get_mut(self) } } -- cgit v1.2.3