memoize
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.
Read the documentation (cargo doc --open
) for the sparse details, or take a
look at the examples/
, if you want to know more:
// From examples/test2.rs
use memoize;
This is expanded into (with a few simplifications):
// This is obviously further expanded before compiling.
lazy_static!
Further Functionality
You can choose to use an LRU cache. In fact, if you know that a memoized function has an unbounded number of different inputs, you should do this! In that case, use the attribute like this:
// From examples/test1.rs
// Compile with --features=full
use memoize;
Adding more caches and configuration options is relatively simple, and a matter
of parsing attribute parameters. Currently, compiling will fail if you use a
parameter such as Capacity
without the feature full
being enabled.
Another parameter is TimeToLive, specifying how long a cached value is allowed to live:
chrono::Duration
is also possible, but would have to first be converted to
std::time::Duration
The cached value will never be older than duration provided and instead recalculated on the next request.
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 sometimes a bit slow to catch up here :) -- Lewin