function_cache

Crates.iofunction_cache
lib.rsfunction_cache
version0.1.0
sourcesrc
created_at2021-07-01 16:28:10.870315
updated_at2021-07-01 16:28:10.870315
descriptionA type that automatically caches the result of a function.
homepagehttps://github.com/MattBolitho/function_cache
repositoryhttps://github.com/MattBolitho/function_cache
max_upload_size
id417509
size6,638
Matt Bolitho (MattBolitho)

documentation

README

function_cache

function_cache is a simple generic wrapper around a function that caches results for a given input to a hash map.

It is based on the example from chapter 13.1 of the Rust book and serves as a small example for me to better understand the Cargo package ecosystem!

Install

To install, simple add the function_cache crate your Cargo.toml file:

[dependencies]
function_cache = "0.1.0"

Example

CachedFunction instances can be created with the new static method, which takes a closure. The underlying value can be accessed with the value(arg) method.

let mut cached_function = CachedFunction::new(|x: i32| {
    thread::sleep(Duration::from_secs(5));
    x
});

let not_cached = cached_function.value(2);  // returns 2, after 5 seconds
let cached = cached_function.value(2); // returns 2, but much quicker!

License

Distributed under the MIT License. See LICENSE.md for more information.

Commit count: 0

cargo fmt