Mercurial > lbo > hg > memoize
changeset 96:528ce50ec228
Applied comments by @dermesser
author | blyxyas <blyxyas@gmail.com> |
---|---|
date | Sun, 15 Jan 2023 13:15:04 +0100 |
parents | 3d9facf8c639 |
children | 525fcf2058ae e3495f149cdd |
files | Cargo.toml examples/ahash.rs examples/custom_hasher.rs examples/full_featured.rs examples/fxhash.rs examples/patterns.rs inner/src/lib.rs src/lib.rs |
diffstat | 8 files changed, 50 insertions(+), 43 deletions(-) [+] |
line wrap: on
line diff
--- a/Cargo.toml Fri Jan 13 23:10:50 2023 +0100 +++ b/Cargo.toml Sun Jan 15 13:15:04 2023 +0100 @@ -25,5 +25,5 @@ members = ["inner/"] [features] -default = [] +default = ["full"] full = ["lru", "memoize-inner/full"]
--- a/examples/ahash.rs Fri Jan 13 23:10:50 2023 +0100 +++ b/examples/ahash.rs Sun Jan 15 13:15:04 2023 +0100 @@ -1,6 +1,5 @@ - +use ahash::{HashMap, HashMapExt}; use memoize::memoize; -use ahash::{HashMap, HashMapExt}; #[cfg(feature = "full")] #[memoize(CustomHasher: HashMap)] @@ -21,6 +20,5 @@ #[cfg(not(feature = "full"))] fn main() { - println!("Use the \"full\" feature to execute this example"); + println!("Use the \"full\" feature to execute this example"); } -
--- a/examples/custom_hasher.rs Fri Jan 13 23:10:50 2023 +0100 +++ b/examples/custom_hasher.rs Sun Jan 15 13:15:04 2023 +0100 @@ -9,6 +9,13 @@ true } +// ! This will panic because CustomHasher and Capacity are being used. +// #[cfg(feature = "full")] +// #[memoize(CustomHasher: std::collections::HashMap, Capacity: 3usize)] +// fn will_panic(a: u32, b: u32) -> u32 { +// a + b +// } + #[cfg(feature = "full")] fn main() { // `hello` is only called once here. @@ -21,6 +28,5 @@ #[cfg(not(feature = "full"))] fn main() { - println!("Use the \"full\" feature to execute this example"); + println!("Use the \"full\" feature to execute this example"); } -
--- a/examples/full_featured.rs Fri Jan 13 23:10:50 2023 +0100 +++ b/examples/full_featured.rs Sun Jan 15 13:15:04 2023 +0100 @@ -38,4 +38,4 @@ println!("result: {:?}", hello("ABC".to_string())); // Same as refreshed println!("result: {:?}", memoized_original_hello("ABC".to_string())); } -} \ No newline at end of file +}
--- a/examples/fxhash.rs Fri Jan 13 23:10:50 2023 +0100 +++ b/examples/fxhash.rs Sun Jan 15 13:15:04 2023 +0100 @@ -1,4 +1,3 @@ - use memoize::memoize; use rustc_hash::FxHashMap; @@ -21,6 +20,5 @@ #[cfg(not(feature = "full"))] fn main() { - println!("Use the \"full\" feature to execute this example"); + println!("Use the \"full\" feature to execute this example"); } -
--- a/examples/patterns.rs Fri Jan 13 23:10:50 2023 +0100 +++ b/examples/patterns.rs Sun Jan 15 13:15:04 2023 +0100 @@ -19,7 +19,7 @@ fn main() { // `manhattan_distance` is only called once here. assert_eq!(manhattan_distance((1, 1), (1, 3)), 2); - + // Same with `get_value`. assert_eq!(get_value(OnlyOne::Value(0)), 0); }
--- a/inner/src/lib.rs Fri Jan 13 23:10:50 2023 +0100 +++ b/inner/src/lib.rs Sun Jan 15 13:15:04 2023 +0100 @@ -11,7 +11,7 @@ syn::custom_keyword!(TimeToLive); syn::custom_keyword!(SharedCache); syn::custom_keyword!(CustomHasher); - syn::custom_keyword!(HasherInit); + syn::custom_keyword!(HasherInit); syn::custom_punctuation!(Colon, :); } @@ -72,12 +72,12 @@ let cap: syn::Path = input.parse().unwrap(); return Ok(CacheOption::CustomHasher(cap)); } - if la.peek(kw::HasherInit) { - input.parse::<kw::HasherInit>().unwrap(); - input.parse::<kw::Colon>().unwrap(); - let cap: syn::ExprCall = input.parse().unwrap(); - return Ok(CacheOption::HasherInit(cap)); - } + if la.peek(kw::HasherInit) { + input.parse::<kw::HasherInit>().unwrap(); + input.parse::<kw::Colon>().unwrap(); + let cap: syn::ExprCall = input.parse().unwrap(); + return Ok(CacheOption::HasherInit(cap)); + } Err(la.error()) } } @@ -93,9 +93,7 @@ CacheOption::LRUMaxEntries(cap) => opts.lru_max_entries = Some(cap), CacheOption::TimeToLive(sec) => opts.time_to_live = Some(sec), CacheOption::CustomHasher(hasher) => opts.custom_hasher = Some(hasher), - CacheOption::HasherInit(init) => { - opts.custom_hasher_initializer = Some(init) - } + CacheOption::HasherInit(init) => opts.custom_hasher_initializer = Some(init), CacheOption::SharedCache => opts.shared_cache = true, } } @@ -121,11 +119,12 @@ quote::quote! { #hasher<#key_type, #value_type> }, quote::quote! { #hasher::new() }, ); + } else { + ( + quote::quote! { std::collections::HashMap<#key_type, #value_type> }, + quote::quote! { std::collections::HashMap::new() }, + ) } - ( - quote::quote! { std::collections::HashMap<#key_type, #value_type> }, - quote::quote! { std::collections::HashMap::new() }, - ) } /// Returns names of methods as TokenStreams to insert and get (respectively) elements from a @@ -160,28 +159,34 @@ // This is the unbounded default. match options.lru_max_entries { None => { - if let Some(hasher) = &options.custom_hasher { + if let Some(hasher) = &options.custom_hasher { if let Some(hasher_init) = &options.custom_hasher_initializer { - return ( - quote::quote! { #hasher<#key_type, #value_type> }, - quote::quote! { #hasher_init }, - ); - } else { - return ( - quote::quote! { #hasher<#key_type, #value_type> }, - quote::quote! { #hasher::new() }, - ) - } + return ( + quote::quote! { #hasher<#key_type, #value_type> }, + quote::quote! { #hasher_init }, + ); + } else { + return ( + quote::quote! { #hasher<#key_type, #value_type> }, + quote::quote! { #hasher::new() }, + ); + } } ( quote::quote! { std::collections::HashMap<#key_type, #value_type> }, quote::quote! { std::collections::HashMap::new() }, ) } - Some(cap) => ( - quote::quote! { ::memoize::lru::LruCache<#key_type, #value_type> }, - quote::quote! { ::memoize::lru::LruCache::new(#cap) }, - ), + Some(cap) => { + if let Some(_) = &options.custom_hasher { + panic!("You can't use LRUMaxEntries and a Custom Hasher. Remove `LRUMaxEntries` from the attribute"); + } else { + ( + quote::quote! { ::memoize::lru::LruCache<#key_type, #value_type> }, + quote::quote! { ::memoize::lru::LruCache::new(#cap) }, + ) + } + } } } @@ -238,7 +243,7 @@ * `#[memoize(TimeToLive: Duration::from_secs(2))]`. In that case, cached value will be actual * no longer than duration provided and refreshed with next request. If you prefer chrono::Duration, * it can be also used: `#[memoize(TimeToLive: chrono::Duration::hours(9).to_std().unwrap()]` - * + * * You can also specify a custom hasher: `#[memoize(CustomHasher: ahash::HashMap)]`, as some hashers don't use a `new()` method to initialize them, you can also specifiy a `HasherInit` parameter, like this: `#[memoize(CustomHasher: FxHashMap, HasherInit: FxHashMap::default())]`, so it will initialize your `FxHashMap` with `FxHashMap::default()` insteado of `FxHashMap::new()` * * This mechanism can, in principle, be extended (in the source code) to any other cache mechanism.