Mercurial > lbo > hg > memoize
changeset 93:db1c1e6b5642
Merge pull request #22 from blyxyas/master
[#21] Fix possible name overlapping
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Fri, 13 Jan 2023 17:21:02 +0100 |
parents | 940bd9c96aa5 (current diff) 813907a7c64f (diff) |
children | 7d8d8525f957 525fcf2058ae |
files | |
diffstat | 3 files changed, 33 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Fri Jan 13 17:21:02 2023 +0100 @@ -0,0 +1,2 @@ +target +Cargo.lock \ No newline at end of file
--- a/README.md Sun Dec 25 12:21:25 2022 +0100 +++ b/README.md Fri Jan 13 17:21:02 2023 +0100 @@ -46,19 +46,19 @@ #[allow(unused_variables)] fn hello(arg: String, arg2: usize) -> bool { - let r = MEMOIZED_MAPPING_HELLO.with(|hm| { - let mut hm = hm.borrow_mut(); - hm.get(&(arg.clone(), arg2.clone())).cloned() + let ATTR_MEMOIZE_RETURN__ = MEMOIZED_MAPPING_HELLO.with(|ATTR_MEMOIZE_HM__| { + let mut ATTR_MEMOIZE_HM__ = ATTR_MEMOIZE_HM__.borrow_mut(); + ATTR_MEMOIZE_HM__.get(&(arg.clone(), arg2.clone())).cloned() }); - if let Some(r) = r { - return r; + if let Some(ATTR_MEMOIZE_RETURN__) = ATTR_MEMOIZE_RETURN__ { + return ATTR_MEMOIZE_RETURN__; } - let r = memoized_original_hello(arg.clone(), arg2.clone()); + let ATTR_MEMOIZE_RETURN__ = memoized_original_hello(arg.clone(), arg2.clone()); - MEMOIZED_MAPPING_HELLO.with(|hm| { - let mut hm = hm.borrow_mut(); - hm.insert((arg, arg2), r.clone()); + MEMOIZED_MAPPING_HELLO.with(|ATTR_MEMOIZE_HM__| { + let mut ATTR_MEMOIZE_HM__ = ATTR_MEMOIZE_HM__.borrow_mut(); + ATTR_MEMOIZE_HM__.insert((arg, arg2), ATTR_MEMOIZE_RETURN__.clone()); }); r
--- a/inner/src/lib.rs Sun Dec 25 12:21:25 2022 +0100 +++ b/inner/src/lib.rs Fri Jan 13 17:21:02 2023 +0100 @@ -221,7 +221,7 @@ Ok((t, n)) => { input_types = t; input_names = n; - }, + } Err(e) => return e.to_compile_error().into(), } @@ -265,52 +265,52 @@ let (insert_fn, get_fn) = store::cache_access_methods(&options); let (read_memo, memoize) = match options.time_to_live { None => ( - quote::quote!(hm.#get_fn(&#syntax_names_tuple_cloned).cloned()), - quote::quote!(hm.#insert_fn(#syntax_names_tuple, r.clone());), + quote::quote!(ATTR_MEMOIZE_HM__.#get_fn(&#syntax_names_tuple_cloned).cloned()), + quote::quote!(ATTR_MEMOIZE_HM__.#insert_fn(#syntax_names_tuple, ATTR_MEMOIZE_RETURN__.clone());), ), Some(ttl) => ( quote::quote! { - hm.#get_fn(&#syntax_names_tuple_cloned).and_then(|(last_updated, r)| - (last_updated.elapsed() < #ttl).then(|| r.clone()) + ATTR_MEMOIZE_HM__.#get_fn(&#syntax_names_tuple_cloned).and_then(|(last_updated, ATTR_MEMOIZE_RETURN__)| + (last_updated.elapsed() < #ttl).then(|| ATTR_MEMOIZE_RETURN__.clone()) ) }, - quote::quote!(hm.#insert_fn(#syntax_names_tuple, (std::time::Instant::now(), r.clone()));), + quote::quote!(ATTR_MEMOIZE_HM__.#insert_fn(#syntax_names_tuple, (std::time::Instant::now(), ATTR_MEMOIZE_RETURN__.clone()));), ), }; let memoizer = if options.shared_cache { quote::quote! { { - let mut hm = #store_ident.lock().unwrap(); - if let Some(r) = #read_memo { - return r + let mut ATTR_MEMOIZE_HM__ = #store_ident.lock().unwrap(); + if let Some(ATTR_MEMOIZE_RETURN__) = #read_memo { + return ATTR_MEMOIZE_RETURN__ } } - let r = #memoized_id(#(#input_names.clone()),*); + let ATTR_MEMOIZE_RETURN__ = #memoized_id(#(#input_names.clone()),*); - let mut hm = #store_ident.lock().unwrap(); + let mut ATTR_MEMOIZE_HM__ = #store_ident.lock().unwrap(); #memoize - r + ATTR_MEMOIZE_RETURN__ } } else { quote::quote! { - let r = #store_ident.with(|hm| { - let mut hm = hm.borrow_mut(); + let ATTR_MEMOIZE_RETURN__ = #store_ident.with(|ATTR_MEMOIZE_HM__| { + let mut ATTR_MEMOIZE_HM__ = ATTR_MEMOIZE_HM__.borrow_mut(); #read_memo }); - if let Some(r) = r { - return r; + if let Some(ATTR_MEMOIZE_RETURN__) = ATTR_MEMOIZE_RETURN__ { + return ATTR_MEMOIZE_RETURN__; } - let r = #memoized_id(#(#input_names.clone()),*); + let ATTR_MEMOIZE_RETURN__ = #memoized_id(#(#input_names.clone()),*); - #store_ident.with(|hm| { - let mut hm = hm.borrow_mut(); + #store_ident.with(|ATTR_MEMOIZE_HM__| { + let mut ATTR_MEMOIZE_HM__ = ATTR_MEMOIZE_HM__.borrow_mut(); #memoize }); - r + ATTR_MEMOIZE_RETURN__ } }; @@ -318,7 +318,7 @@ let flusher = quote::quote! { #vis fn #flush_name() { - #store_ident.with(|hm| hm.borrow_mut().clear()); + #store_ident.with(|ATTR_MEMOIZE_HM__| ATTR_MEMOIZE_HM__.borrow_mut().clear()); } }; @@ -342,10 +342,7 @@ return Ok((vec![], vec![])); } if let syn::FnArg::Receiver(_) = sig.inputs[0] { - return Err(syn::Error::new( - sig.span(), - "Cannot memoize methods!", - )); + return Err(syn::Error::new(sig.span(), "Cannot memoize methods!")); } let mut types = vec![];