Function rustc::util::common::memoizedUnstable [-] [+] [src]

pub fn memoized<T, U, S, F>(cache: &RefCell<HashMap<T, U, S>>, arg: T, f: F) -> U where T: Clone + Hash + Eq, U: Clone, S: HashState, F: FnOnce(T) -> U

Memoizes a one-argument closure using the given RefCell containing a type implementing MutableMap to serve as a cache.

In the future the signature of this function is expected to be: pub fn memoized<T: Clone, U: Clone, M: MutableMap<T, U>>( cache: &RefCell<M>, f: &|T| -> U ) -> impl |T| -> U { but currently it is not possible.

Examples

struct Context {
   cache: RefCell<HashMap<usize, usize>>
}

fn factorial(ctxt: &Context, n: usize) -> usize {
    memoized(&ctxt.cache, n, |n| match n {
        0 | 1 => n,
        _ => factorial(ctxt, n - 2) + factorial(ctxt, n - 1)
    })
}