From 58abf5872023aca7ee6459fa3b2e067d57923ba5 Mon Sep 17 00:00:00 2001 From: Mica White Date: Sun, 9 Mar 2025 20:49:56 -0400 Subject: Finish testing and fixing --- src/mutex.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/mutex.rs') diff --git a/src/mutex.rs b/src/mutex.rs index 2022501..413bd8a 100644 --- a/src/mutex.rs +++ b/src/mutex.rs @@ -136,10 +136,7 @@ pub struct Mutex { /// A reference to a mutex that unlocks it when dropped. /// /// This is similar to [`MutexGuard`], except it does not hold a [`Keyable`]. -pub struct MutexRef<'a, T: ?Sized + 'a, R: RawMutex>( - &'a Mutex, - PhantomData<(&'a mut T, R::GuardMarker)>, -); +pub struct MutexRef<'a, T: ?Sized + 'a, R: RawMutex>(&'a Mutex, PhantomData); /// An RAII implementation of a “scoped lock” of a mutex. /// @@ -182,6 +179,26 @@ mod tests { drop(guard) } + #[test] + fn from_works() { + let key = ThreadKey::get().unwrap(); + let mutex: crate::Mutex<_> = Mutex::from("Hello, world!"); + + let guard = mutex.lock(key); + assert_eq!(*guard, "Hello, world!"); + } + + #[test] + fn as_mut_works() { + let key = ThreadKey::get().unwrap(); + let mut mutex = crate::Mutex::from(42); + + let mut_ref = mutex.as_mut(); + *mut_ref = 24; + + mutex.scoped_lock(key, |guard| assert_eq!(*guard, 24)) + } + #[test] fn display_works_for_guard() { let key = ThreadKey::get().unwrap(); -- cgit v1.2.3