@@ -10,7 +10,6 @@ use syn::{parse_macro_input, parse_str, Block, Ident, ItemFn, ReturnType, Type};
10
10
#[ derive( Debug , Default , FromMeta , Eq , PartialEq ) ]
11
11
enum SyncWriteMode {
12
12
#[ default]
13
- Disabled ,
14
13
Default ,
15
14
ByKey ,
16
15
}
@@ -36,7 +35,7 @@ struct MacroArgs {
36
35
#[ darling( default ) ]
37
36
option : bool ,
38
37
#[ darling( default ) ]
39
- sync_writes : SyncWriteMode ,
38
+ sync_writes : Option < SyncWriteMode > ,
40
39
#[ darling( default ) ]
41
40
with_cached_flag : bool ,
42
41
#[ darling( default ) ]
@@ -199,7 +198,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
199
198
_ => panic ! ( "the result and option attributes are mutually exclusive" ) ,
200
199
} ;
201
200
202
- if args. result_fallback && args. sync_writes != SyncWriteMode :: Disabled {
201
+ if args. result_fallback && args. sync_writes . is_some ( ) {
203
202
panic ! ( "result_fallback and sync_writes are mutually exclusive" ) ;
204
203
}
205
204
@@ -216,7 +215,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
216
215
let ty;
217
216
if asyncness. is_some ( ) {
218
217
lock = match args. sync_writes {
219
- SyncWriteMode :: ByKey => quote ! {
218
+ Some ( SyncWriteMode :: ByKey ) => quote ! {
220
219
let mut locks = #cache_ident. lock( ) . await ;
221
220
let lock = locks
222
221
. entry( key. clone( ) )
@@ -239,7 +238,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
239
238
} ;
240
239
241
240
ty = match args. sync_writes {
242
- SyncWriteMode :: ByKey => quote ! {
241
+ Some ( SyncWriteMode :: ByKey ) => quote ! {
243
242
#visibility static #cache_ident: :: cached:: once_cell:: sync:: Lazy <:: cached:: async_sync:: Mutex <std:: collections:: HashMap <#cache_key_ty, std:: sync:: Arc <:: cached:: async_sync:: Mutex <#cache_ty>>>>> = :: cached:: once_cell:: sync:: Lazy :: new( || :: cached:: async_sync:: Mutex :: new( std:: collections:: HashMap :: new( ) ) ) ;
244
243
} ,
245
244
_ => quote ! {
@@ -248,7 +247,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
248
247
} ;
249
248
} else {
250
249
lock = match args. sync_writes {
251
- SyncWriteMode :: ByKey => quote ! {
250
+ Some ( SyncWriteMode :: ByKey ) => quote ! {
252
251
let mut locks = #cache_ident. lock( ) . unwrap( ) ;
253
252
let lock = locks. entry( key. clone( ) ) . or_insert_with( || std:: sync:: Arc :: new( std:: sync:: Mutex :: new( #cache_create) ) ) . clone( ) ;
254
253
drop( locks) ;
@@ -268,7 +267,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
268
267
} ;
269
268
270
269
ty = match args. sync_writes {
271
- SyncWriteMode :: ByKey => quote ! {
270
+ Some ( SyncWriteMode :: ByKey ) => quote ! {
272
271
#visibility static #cache_ident: :: cached:: once_cell:: sync:: Lazy <std:: sync:: Mutex <std:: collections:: HashMap <#cache_key_ty, std:: sync:: Arc <std:: sync:: Mutex <#cache_ty>>>>> = :: cached:: once_cell:: sync:: Lazy :: new( || std:: sync:: Mutex :: new( std:: collections:: HashMap :: new( ) ) ) ;
273
272
} ,
274
273
_ => quote ! {
@@ -285,7 +284,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream {
285
284
#set_cache_and_return
286
285
} ;
287
286
288
- let do_set_return_block = if args. sync_writes != SyncWriteMode :: Disabled {
287
+ let do_set_return_block = if args. sync_writes . is_some ( ) {
289
288
quote ! {
290
289
#lock
291
290
if let Some ( result) = cache. cache_get( & key) {
0 commit comments