@@ -3,6 +3,7 @@ use crate::sync::{AtomicBool, ReadGuard, RwLock, WriteGuard};
33use crate :: sync:: { DynSend , DynSync } ;
44use std:: {
55 cell:: UnsafeCell ,
6+ marker:: PhantomData ,
67 ops:: { Deref , DerefMut } ,
78 sync:: atomic:: Ordering ,
89} ;
@@ -37,10 +38,7 @@ impl<T> FreezeLock<T> {
3738 } else {
3839 Some ( self . lock . read ( ) )
3940 } ,
40- // SAFETY: If this is not frozen, `_lock_guard` holds the lock to the `UnsafeCell` so
41- // this has shared access until the `FreezeReadGuard` is dropped. If this is frozen,
42- // the data cannot be modified and shared access is sound.
43- data : unsafe { & * self . data . get ( ) } ,
41+ lock : self ,
4442 }
4543 }
4644
@@ -50,12 +48,7 @@ impl<T> FreezeLock<T> {
5048 let _lock_guard = self . lock . write ( ) ;
5149 // Use relaxed ordering since we're in the write lock.
5250 assert ! ( !self . frozen. load( Ordering :: Relaxed ) , "still mutable" ) ;
53- FreezeWriteGuard {
54- _lock_guard,
55- // SAFETY: `_lock_guard` holds the lock to the `UnsafeCell` so this has mutable access
56- // until the `FreezeWriteGuard` is dropped.
57- data : unsafe { & mut * self . data . get ( ) } ,
58- }
51+ FreezeWriteGuard { _lock_guard, lock : self , marker : PhantomData }
5952 }
6053
6154 #[ inline]
@@ -75,35 +68,41 @@ impl<T> FreezeLock<T> {
7568#[ must_use = "if unused the FreezeLock may immediately unlock" ]
7669pub struct FreezeReadGuard < ' a , T > {
7770 _lock_guard : Option < ReadGuard < ' a , ( ) > > ,
78- data : & ' a T ,
71+ lock : & ' a FreezeLock < T > ,
7972}
8073
8174impl < ' a , T : ' a > Deref for FreezeReadGuard < ' a , T > {
8275 type Target = T ;
8376 #[ inline]
8477 fn deref ( & self ) -> & T {
85- self . data
78+ // SAFETY: If `lock` is not frozen, `_lock_guard` holds the lock to the `UnsafeCell` so
79+ // this has shared access until the `FreezeReadGuard` is dropped. If `lock` is frozen,
80+ // the data cannot be modified and shared access is sound.
81+ unsafe { & * self . lock . data . get ( ) }
8682 }
8783}
8884
8985/// A guard holding mutable access to a `FreezeLock` which is in a locked state or frozen.
9086#[ must_use = "if unused the FreezeLock may immediately unlock" ]
9187pub struct FreezeWriteGuard < ' a , T > {
9288 _lock_guard : WriteGuard < ' a , ( ) > ,
93- data : & ' a mut T ,
89+ lock : & ' a FreezeLock < T > ,
90+ marker : PhantomData < & ' a mut T > ,
9491}
9592
9693impl < ' a , T : ' a > Deref for FreezeWriteGuard < ' a , T > {
9794 type Target = T ;
9895 #[ inline]
9996 fn deref ( & self ) -> & T {
100- self . data
97+ // SAFETY: `self._lock_guard` holds the lock to the `UnsafeCell` so this has shared access.
98+ unsafe { & * self . lock . data . get ( ) }
10199 }
102100}
103101
104102impl < ' a , T : ' a > DerefMut for FreezeWriteGuard < ' a , T > {
105103 #[ inline]
106104 fn deref_mut ( & mut self ) -> & mut T {
107- self . data
105+ // SAFETY: `self._lock_guard` holds the lock to the `UnsafeCell` so this has mutable access.
106+ unsafe { & mut * self . lock . data . get ( ) }
108107 }
109108}
0 commit comments