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 数据库实现
每月98次下载
在 sketch-2d 中使用
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