Mercurial > lbo > hg > memoize
changeset 14:4603aaa0378c
Further improve README
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Thu, 15 Oct 2020 14:28:34 +0200 |
parents | 0b9fe6ecae86 |
children | a794b6862ef4 |
files | Cargo.toml README.md |
diffstat | 2 files changed, 25 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/Cargo.toml Thu Oct 15 14:22:11 2020 +0200 +++ b/Cargo.toml Thu Oct 15 14:28:34 2020 +0200 @@ -1,6 +1,6 @@ [package] name = "memoize" -version = "0.1.1" +version = "0.1.2" description = "Attribute macro for auto-memoizing functions with somewhat-simple signatures" tags = ["memoization", "cache"] authors = ["Lewin Bormann <lewin@lewin-bormann.info>"]
--- a/README.md Thu Oct 15 14:22:11 2020 +0200 +++ b/README.md Thu Oct 15 14:28:34 2020 +0200 @@ -1,5 +1,8 @@ # memoize +![Docs.rs](https://docs.rs/mio/badge.svg) +![Crates.rs](https://img.shields.io/crates/v/memoize.svg) + A `#[memoize]` attribute for somewhat simple Rust functions: That is, functions with one or more `Clone`-able arguments, and a `Clone`-able return type. That's it. @@ -7,20 +10,25 @@ look at the `examples/`, if you want to know more: ```rust +// From examples/test2.rs + use memoize::memoize; + #[memoize] fn hello(arg: String, arg2: usize) -> bool { - arg.len()%2 == arg2 + arg.len()%2 == arg2 } -// `hello` is only called once here. -assert!(! hello("World".to_string(), 0)); -assert!(! hello("World".to_string(), 0)); -// Sometimes one might need the original function. -assert!(! memoized_original_hello("World".to_string(), 0)); +fn main() { + // `hello` is only called once here. + assert!(! hello("World".to_string(), 0)); + assert!(! hello("World".to_string(), 0)); + // Sometimes one might need the original function. + assert!(! memoized_original_hello("World".to_string(), 0)); +} ``` -This is, aside from the `assert`s, expanded into: +This is expanded into (with a few simplifications): ```rust // This is obviously further expanded before compiling. @@ -29,25 +37,24 @@ } fn memoized_original_hello(arg: String, arg2: usize) -> bool { - arg.len() % 2 == arg2 + arg.len() % 2 == arg2 } fn hello(arg: String, arg2: usize) -> bool { - let mut hm = &mut MEMOIZED_MAPPING_HELLO.lock().unwrap(); - if let Some(r) = hm.get(&(arg.clone(), arg2.clone())) { - return r.clone(); - } - let r = memoized_original_hello(arg.clone(), arg2.clone()); - hm.insert((arg, arg2), r.clone()); - r + let mut hm = &mut MEMOIZED_MAPPING_HELLO.lock().unwrap(); + if let Some(r) = hm.get(&(arg.clone(), arg2.clone())) { + return r.clone(); + } + let r = memoized_original_hello(arg.clone(), arg2.clone()); + hm.insert((arg, arg2), r.clone()); + r } - ``` ## Contributions ...are always welcome! This being my first procedural-macros crate, I am grateful for improvements of functionality and style. Please send a pull -request, and don't be discouraged if it takes a while for me to review it -- I'm +request, and don't be discouraged if it takes a while for me to review it; I'm sometimes a bit slow to catch up here :) -- Lewin