#closures #key-value-store #persistent #dynamic #storing #return #hash-map

fn-store

A dynamic persistent value store using closure type as key and storing its return value

10 releases (3 stable)

new 1.2.1 Apr 20, 2024
1.1.0 Jan 9, 2024
1.0.0 Jul 6, 2023
0.3.1 Jul 5, 2023
0.1.5 Jul 1, 2023

#54 in Database implementations

Download history 9/week @ 2024-01-08 55/week @ 2024-02-19 12/week @ 2024-02-26 10/week @ 2024-03-11 65/week @ 2024-04-01 208/week @ 2024-04-15

273 downloads per month
Used in sketch-2d

MIT license

11KB
187 lines

FnStore

FnStore is a abstraction around the HashMap, like TypeMap. But uses closure's type(each closure's type is unique in Rust) as key and stores produced value. Allowing to be used like effective low cost dependency injection container.

Usage

use fn_store::LocalFnStore;

let mut store = LocalFnStore::new();

fn one() -> i32 {
    println!("one computed");
    1
}

// get or compute(and insert) value using given closure. The closure depends on value of `one` function to compute its output.
let a = *store.get(|| store.get(one) + 1);
dbg!(a);

// b is *not* a because each closure's type is unique
let b = *store.get(|| store.get(one) + 1);
dbg!(b);

// get or compute(and insert) value using give function. But will not compute since it is computed already when producing a.
let c = *store.get(one);
dbg!(c);

will output

one computed
a = 2
b = 2
c = 1

License

MIT

Dependencies

~2–8.5MB
~39K SLoC