From e513e1cada4b175a9346ba8dda90c891d5883b75 Mon Sep 17 00:00:00 2001 From: Manuel Raimann Date: Thu, 19 Sep 2024 17:04:51 +0200 Subject: [PATCH] Replace Disabled SyncWriteMode with Option --- cached_proc_macro/src/cached.rs | 15 +++++++-------- cached_proc_macro/src/once.rs | 7 +++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/cached_proc_macro/src/cached.rs b/cached_proc_macro/src/cached.rs index b4025d5..4eec8b8 100644 --- a/cached_proc_macro/src/cached.rs +++ b/cached_proc_macro/src/cached.rs @@ -10,7 +10,6 @@ use syn::{parse_macro_input, parse_str, Block, Ident, ItemFn, ReturnType, Type}; #[derive(Debug, Default, FromMeta, Eq, PartialEq)] enum SyncWriteMode { #[default] - Disabled, Default, ByKey, } @@ -36,7 +35,7 @@ struct MacroArgs { #[darling(default)] option: bool, #[darling(default)] - sync_writes: SyncWriteMode, + sync_writes: Option, #[darling(default)] with_cached_flag: bool, #[darling(default)] @@ -199,7 +198,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { _ => panic!("the result and option attributes are mutually exclusive"), }; - if args.result_fallback && args.sync_writes != SyncWriteMode::Disabled { + if args.result_fallback && args.sync_writes.is_some() { panic!("result_fallback and sync_writes are mutually exclusive"); } @@ -216,7 +215,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { let ty; if asyncness.is_some() { lock = match args.sync_writes { - SyncWriteMode::ByKey => quote! { + Some(SyncWriteMode::ByKey) => quote! { let mut locks = #cache_ident.lock().await; let lock = locks .entry(key.clone()) @@ -239,7 +238,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { }; ty = match args.sync_writes { - SyncWriteMode::ByKey => quote! { + Some(SyncWriteMode::ByKey) => quote! { #visibility static #cache_ident: ::cached::once_cell::sync::Lazy<::cached::async_sync::Mutex>>>> = ::cached::once_cell::sync::Lazy::new(|| ::cached::async_sync::Mutex::new(std::collections::HashMap::new())); }, _ => quote! { @@ -248,7 +247,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { }; } else { lock = match args.sync_writes { - SyncWriteMode::ByKey => quote! { + Some(SyncWriteMode::ByKey) => quote! { let mut locks = #cache_ident.lock().unwrap(); let lock = locks.entry(key.clone()).or_insert_with(|| std::sync::Arc::new(std::sync::Mutex::new(#cache_create))).clone(); drop(locks); @@ -268,7 +267,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { }; ty = match args.sync_writes { - SyncWriteMode::ByKey => quote! { + Some(SyncWriteMode::ByKey) => quote! { #visibility static #cache_ident: ::cached::once_cell::sync::Lazy>>>> = ::cached::once_cell::sync::Lazy::new(|| std::sync::Mutex::new(std::collections::HashMap::new())); }, _ => quote! { @@ -285,7 +284,7 @@ pub fn cached(args: TokenStream, input: TokenStream) -> TokenStream { #set_cache_and_return }; - let do_set_return_block = if args.sync_writes != SyncWriteMode::Disabled { + let do_set_return_block = if args.sync_writes.is_some() { quote! { #lock if let Some(result) = cache.cache_get(&key) { diff --git a/cached_proc_macro/src/once.rs b/cached_proc_macro/src/once.rs index fd2ac07..907f74b 100644 --- a/cached_proc_macro/src/once.rs +++ b/cached_proc_macro/src/once.rs @@ -9,7 +9,6 @@ use syn::{parse_macro_input, Ident, ItemFn, ReturnType}; #[derive(Debug, Default, FromMeta)] enum SyncWriteMode { #[default] - Disabled, Default, } @@ -20,7 +19,7 @@ struct OnceMacroArgs { #[darling(default)] time: Option, #[darling(default)] - sync_writes: SyncWriteMode, + sync_writes: Option, #[darling(default)] result: bool, #[darling(default)] @@ -228,7 +227,7 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream { }; let do_set_return_block = match args.sync_writes { - SyncWriteMode::Default => quote! { + Some(SyncWriteMode::Default) => quote! { #r_lock_return_cache_block #w_lock if let Some(result) = &*cached { @@ -237,7 +236,7 @@ pub fn once(args: TokenStream, input: TokenStream) -> TokenStream { #function_call #set_cache_and_return }, - SyncWriteMode::Disabled => quote! { + None => quote! { #r_lock_return_cache_block #function_call #w_lock