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

fn-store

使用闭包类型作为键并存储其返回值的动态持久化值存储

10个版本 (3个稳定版本)

1.2.1 2024年4月20日
1.1.0 2024年1月9日
1.0.0 2023年7月6日
0.3.1 2023年7月5日
0.1.5 2023年7月1日

#67 in 数据库实现

Download history 208/week @ 2024-04-15 30/week @ 2024-04-22 5/week @ 2024-05-20 2/week @ 2024-06-10 24/week @ 2024-07-01 74/week @ 2024-07-29

每月98次下载
sketch-2d 中使用

MIT 许可证

11KB
187 代码行

FnStore

FnStore是在HashMap之上的抽象,类似于TypeMap。但它使用闭包的类型(每个闭包的类型在Rust中是唯一的)作为键,并存储生成的。允许像有效的低成本依赖注入容器一样使用。

用法

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);

将输出

one computed
a = 2
b = 2
c = 1

许可证

MIT

依赖项

~2–7.5MB
~39K SLoC