小编Bre*_*rby的帖子

在安全的 Rust 中,是否可以实现单线程零成本记忆而不具有可变性?

我有兴趣寻找或实现一个 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

5
推荐指数
0
解决办法
291
查看次数

标签 统计

rust ×1