我有兴趣寻找或实现一个 Rust 数据结构,它提供了一种零成本的方式来记忆具有任意输出类型的单个计算T。具体来说,我想要一个通用类型Cache<T>,其内部数据占用的空间不超过Option<T>,并具有以下基本 API:
impl<T> Cache<T> {
/// Return a new Cache with no value stored in it yet.
pub fn new() -> Self {
// ...
}
/// If the cache has a value stored in it, return a reference to the
/// stored value. Otherwise, compute `f()`, store its output
/// in the cache, and then return a reference to the stored value.
pub fn call<F: FnOnce() -> T>(&self, f: F) -> …Run Code Online (Sandbox Code Playgroud) rust ×1