From 5fb7b96704515d99d955fd35485c33348a551974 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Tue, 13 Jul 2021 10:53:10 +0100 Subject: [PATCH] Replace custom `UserDataRef(Mut)` with standard `Ref`(Mut) --- src/userdata.rs | 78 ++++--------------------------------------------- 1 file changed, 6 insertions(+), 72 deletions(-) diff --git a/src/userdata.rs b/src/userdata.rs index 434bd9a..2e8ca8b 100644 --- a/src/userdata.rs +++ b/src/userdata.rs @@ -608,88 +608,22 @@ impl UserDataCell { } // Immutably borrows the wrapped value. - fn try_borrow(&self) -> Result> { + fn try_borrow(&self) -> Result> { self.0 .try_borrow() - .map(|r| UserDataRef(UserDataRefInner::Ref(r))) + .map(|r| Ref::map(r, |r| r.deref())) .map_err(|_| Error::UserDataBorrowError) } // Mutably borrows the wrapped value. - fn try_borrow_mut(&self) -> Result> { + fn try_borrow_mut(&self) -> Result> { self.0 .try_borrow_mut() - .map(|r| UserDataRefMut(UserDataRefMutInner::Ref(r))) + .map(|r| RefMut::map(r, |r| r.deref_mut())) .map_err(|_| Error::UserDataBorrowMutError) } } -/// A wrapper type for an immutably borrowed value from an `AnyUserData`. -pub struct UserDataRef<'a, T>(UserDataRefInner<'a, T>); - -enum UserDataRefInner<'a, T> { - Ref(Ref<'a, UserDataWrapped>), -} - -/// A wrapper type for a mutably borrowed value from an `AnyUserData`. -pub struct UserDataRefMut<'a, T>(UserDataRefMutInner<'a, T>); - -enum UserDataRefMutInner<'a, T> { - Ref(RefMut<'a, UserDataWrapped>), -} - -impl Deref for UserDataRef<'_, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - match &self.0 { - UserDataRefInner::Ref(x) => &*x, - } - } -} - -impl Deref for UserDataRefMut<'_, T> { - type Target = T; - - fn deref(&self) -> &Self::Target { - match &self.0 { - UserDataRefMutInner::Ref(x) => &*x, - } - } -} - -impl DerefMut for UserDataRefMut<'_, T> { - fn deref_mut(&mut self) -> &mut Self::Target { - match &mut self.0 { - UserDataRefMutInner::Ref(x) => &mut *x, - } - } -} - -impl fmt::Debug for UserDataRef<'_, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&*self as &T, f) - } -} - -impl fmt::Debug for UserDataRefMut<'_, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&*self as &T, f) - } -} - -impl fmt::Display for UserDataRef<'_, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&*self as &T, f) - } -} - -impl fmt::Display for UserDataRefMut<'_, T> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&*self as &T, f) - } -} - pub(crate) enum UserDataWrapped { Default(T), #[cfg(feature = "serialize")] @@ -790,7 +724,7 @@ impl<'lua> AnyUserData<'lua> { /// /// Returns a `UserDataBorrowError` if the userdata is already mutably borrowed. Returns a /// `UserDataTypeMismatch` if the userdata is not of type `T`. - pub fn borrow(&self) -> Result> { + pub fn borrow(&self) -> Result> { self.inspect(|cell| cell.try_borrow()) } @@ -800,7 +734,7 @@ impl<'lua> AnyUserData<'lua> { /// /// Returns a `UserDataBorrowMutError` if the userdata cannot be mutably borrowed. /// Returns a `UserDataTypeMismatch` if the userdata is not of type `T`. - pub fn borrow_mut(&self) -> Result> { + pub fn borrow_mut(&self) -> Result> { self.inspect(|cell| cell.try_borrow_mut()) }