use std::cell::RefCell;
use std::collections::HashMap;
use std::hash::Hash;
pub struct Cache<K: Eq + Hash, V>(
) but we'll still be able to insert new items dynamically.
RefCell<HashMap<K, Box<V>>>,
);
impl<K: Eq + Hash, V> Cache<K, V> {
pub fn new() -> Cache<K, V> {
Cache(RefCell::new(HashMap::new()))
}
pub fn get<F>(&self, index: K, default: F) -> &V
where
F: FnOnce() -> V,
{
unsafe {
let cache = &mut *self.0.as_ptr();
cache.entry(index).or_insert_with(|| Box::new(default()))
}
}
}