11use crate :: prelude:: * ;
22use snafu:: { ResultExt , Snafu } ;
33use std:: { fs:: File , io, ops:: Deref } ;
4- use tokio:: {
5- sync:: { RwLock , RwLockReadGuard , RwLockWriteGuard } ,
6- task:: spawn_blocking,
7- } ;
4+ use tokio:: { sync:: RwLock , task:: spawn_blocking} ;
85
96pub struct DirectoryStructureLock < T : PathsAccess > {
107 paths_access : T ,
@@ -58,8 +55,7 @@ impl<T: PathsAccess> DirectoryStructureLock<T> {
5855 . await
5956 . unwrap ( )
6057 }
61-
62- pub async fn read_ref ( & self ) -> Result < DirectoryStructureGuardReadRef < ' _ , T > , LockError > {
58+ pub async fn with_read < R > ( & self , f : impl AsyncFnOnce ( & T ) -> R ) -> Result < R , LockError > {
6359 let guard = self . lock_file . read ( ) . await ;
6460 let lock_file = guard. try_clone ( ) . context ( HandleCloneFailedSnafu {
6561 path : & self . lock_path ,
@@ -70,14 +66,14 @@ impl<T: PathsAccess> DirectoryStructureLock<T> {
7066 . context ( LockFailedSnafu {
7167 lock_path : & self . lock_path ,
7268 } ) ?;
73-
74- Ok ( DirectoryStructureGuardReadRef {
75- paths_access : & self . paths_access ,
76- guard ,
77- } )
69+ let ret = f ( & self . paths_access ) . await ;
70+ guard . unlock ( ) . context ( LockFailedSnafu {
71+ lock_path : & self . lock_path ,
72+ } ) ? ;
73+ Ok ( ret )
7874 }
7975
80- pub async fn write_ref ( & self ) -> Result < DirectoryStructureGuardWriteRef < ' _ , T > , LockError > {
76+ pub async fn with_write < R > ( & self , f : impl AsyncFnOnce ( & T ) -> R ) -> Result < R , LockError > {
8177 let guard = self . lock_file . write ( ) . await ;
8278 let lock_file = guard. try_clone ( ) . context ( HandleCloneFailedSnafu {
8379 path : & self . lock_path ,
@@ -88,11 +84,11 @@ impl<T: PathsAccess> DirectoryStructureLock<T> {
8884 . context ( LockFailedSnafu {
8985 lock_path : & self . lock_path ,
9086 } ) ?;
91-
92- Ok ( DirectoryStructureGuardWriteRef {
93- paths_access : & self . paths_access ,
94- guard ,
95- } )
87+ let ret = f ( & self . paths_access ) . await ;
88+ guard . unlock ( ) . context ( LockFailedSnafu {
89+ lock_path : & self . lock_path ,
90+ } ) ? ;
91+ Ok ( ret )
9692 }
9793}
9894
@@ -111,44 +107,6 @@ pub enum LockError {
111107 HandleCloneFailed { source : io:: Error , path : PathBuf } ,
112108}
113109
114- pub struct DirectoryStructureGuardReadRef < ' a , T > {
115- paths_access : & ' a T ,
116- guard : RwLockReadGuard < ' a , File > ,
117- }
118-
119- impl < ' a , T > Deref for DirectoryStructureGuardReadRef < ' a , T > {
120- type Target = T ;
121-
122- fn deref ( & self ) -> & Self :: Target {
123- self . paths_access
124- }
125- }
126-
127- impl < ' a , T > Drop for DirectoryStructureGuardReadRef < ' a , T > {
128- fn drop ( & mut self ) {
129- _ = self . guard . unlock ( ) ;
130- }
131- }
132-
133- pub struct DirectoryStructureGuardWriteRef < ' a , T > {
134- paths_access : & ' a T ,
135- guard : RwLockWriteGuard < ' a , File > ,
136- }
137-
138- impl < ' a , T > Deref for DirectoryStructureGuardWriteRef < ' a , T > {
139- type Target = T ;
140-
141- fn deref ( & self ) -> & Self :: Target {
142- self . paths_access
143- }
144- }
145-
146- impl < ' a , T > Drop for DirectoryStructureGuardWriteRef < ' a , T > {
147- fn drop ( & mut self ) {
148- _ = self . guard . unlock ( ) ;
149- }
150- }
151-
152110pub struct DirectoryStructureGuardOwned < T > {
153111 paths_access : T ,
154112 guard : File ,
0 commit comments