Skip to content

Commit

Permalink
Replace Disabled SyncWriteMode with Option
Browse files Browse the repository at this point in the history
  • Loading branch information
raimannma committed Nov 6, 2024
1 parent fbe5d8e commit e513e1c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
15 changes: 7 additions & 8 deletions cached_proc_macro/src/cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -36,7 +35,7 @@ struct MacroArgs {
#[darling(default)]
option: bool,
#[darling(default)]
sync_writes: SyncWriteMode,
sync_writes: Option<SyncWriteMode>,
#[darling(default)]
with_cached_flag: bool,
#[darling(default)]
Expand Down Expand Up @@ -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");
}

Expand All @@ -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())
Expand All @@ -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<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()));
},
_ => quote! {
Expand All @@ -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);
Expand All @@ -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<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()));
},
_ => quote! {
Expand All @@ -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) {
Expand Down
7 changes: 3 additions & 4 deletions cached_proc_macro/src/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use syn::{parse_macro_input, Ident, ItemFn, ReturnType};
#[derive(Debug, Default, FromMeta)]
enum SyncWriteMode {
#[default]
Disabled,
Default,
}

Expand All @@ -20,7 +19,7 @@ struct OnceMacroArgs {
#[darling(default)]
time: Option<u64>,
#[darling(default)]
sync_writes: SyncWriteMode,
sync_writes: Option<SyncWriteMode>,
#[darling(default)]
result: bool,
#[darling(default)]
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit e513e1c

Please sign in to comment.