diff options
| author | Mica White <botahamec@gmail.com> | 2024-12-25 17:58:38 -0500 |
|---|---|---|
| committer | Mica White <botahamec@gmail.com> | 2024-12-25 22:44:03 -0500 |
| commit | 311842d28d1fbb6da945f0d98f1655f77a58abf9 (patch) | |
| tree | 4da3c8ab6fb3732c1ebb11306dab3799decd6b2c /src/poisonable | |
| parent | 37ab873d21ca1fcd43db8d6a26d5bac4f5285f71 (diff) | |
as_mut re-organization
Diffstat (limited to 'src/poisonable')
| -rw-r--r-- | src/poisonable/poisonable.rs | 43 |
1 files changed, 31 insertions, 12 deletions
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<L: Sharable> Sharable for Poisonable<L> { unsafe impl<L: OwnedLockable> OwnedLockable for Poisonable<L> {} +impl<L: LockableGetMut> LockableGetMut for Poisonable<L> { + type Inner<'a> + = PoisonResult<L::Inner<'a>> + 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<L: LockableIntoInner> LockableIntoInner for Poisonable<L> { + type Inner = PoisonResult<L::Inner>; + + fn into_inner(self) -> Self::Inner { + if self.is_poisoned() { + Err(PoisonError::new(self.inner.into_inner())) + } else { + Ok(self.inner.into_inner()) + } + } +} + impl<L> From<L> for Poisonable<L> { fn from(value: L) -> Self { Self::new(value) @@ -524,15 +551,11 @@ impl<L: LockableIntoInner> Poisonable<L> { /// assert_eq!(mutex.into_inner().unwrap(), 0); /// ``` pub fn into_inner(self) -> PoisonResult<L::Inner> { - if self.is_poisoned() { - Err(PoisonError::new(self.inner.into_inner())) - } else { - Ok(self.inner.into_inner()) - } + LockableIntoInner::into_inner(self) } } -impl<L: LockableAsMut + RawLock> Poisonable<L> { +impl<L: LockableGetMut + RawLock> Poisonable<L> { /// Returns a mutable reference to the underlying data. /// /// Since this call borrows the `Poisonable` mutable, no actual locking @@ -555,11 +578,7 @@ impl<L: LockableAsMut + RawLock> Poisonable<L> { /// assert_eq!(*mutex.lock(key).unwrap(), 10); /// ``` pub fn get_mut(&mut self) -> PoisonResult<L::Inner<'_>> { - if self.is_poisoned() { - Err(PoisonError::new(self.inner.as_mut())) - } else { - Ok(self.inner.as_mut()) - } + LockableGetMut::get_mut(self) } } |
