Mercurial > lbo > hg > memoize
changeset 91:946428e4faa5
[#21] Fix possible name overlapping
author | blyxyas <blyxyas@gmail.com> |
---|---|
date | Fri, 13 Jan 2023 11:44:15 +0100 |
parents | 940bd9c96aa5 |
children | 813907a7c64f |
files | .gitignore Cargo.lock README.md inner/src/lib.rs |
diffstat | 4 files changed, 165 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Fri Jan 13 11:44:15 2023 +0100 @@ -0,0 +1,1 @@ +target \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Cargo.lock Fri Jan 13 11:44:15 2023 +0100 @@ -0,0 +1,133 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ahash" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "lru" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999beba7b6e8345721bd280141ed958096a2e4abdf74f67ff4ce49b4b54e47a" +dependencies = [ + "hashbrown", +] + +[[package]] +name = "memoize" +version = "0.3.3" +dependencies = [ + "lazy_static", + "lru", + "memoize-inner", +] + +[[package]] +name = "memoize-inner" +version = "0.3.2" +dependencies = [ + "lazy_static", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "proc-macro2" +version = "1.0.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
--- a/README.md Sun Dec 25 12:21:25 2022 +0100 +++ b/README.md Fri Jan 13 11:44:15 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 11:44:15 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![];