I have a cache-like structure which internally uses a HashMap
:
impl Cache {
fn insert(&mut self, k: u32, v: String) {
self.map.insert(k, v);
}
fn borrow(&self, k: u32) -> Option<&String> {
self.map.get(&k)
}
}
Playground with external mutability
Now I need internal mutability. Since HashMap
does not implement Copy
, my guess is that RefCell
is the path to follow. Writing the insert
method is straight forward but I encountered problems with the borrow-function. I could return a Ref<String>
, but since I'd like to cache the result, I wrote a small Ref
-wrapper:
struct CacheRef<'a> {
borrow: Ref<'a, HashMap<u32, String>>,
value: &'a String,
}
This won't work since value
references borrow
, so the struct can't be constructed. I know that the reference is always valid: The map can't be mutated, because Ref
locks the map. Is it safe to use a raw pointer instead of a reference?
struct CacheRef<'a> {
borrow: Ref<'a, HashMap<u32, String>>,
value: *const String,
}
Am I overlooking something here? Are there better (or faster) options? I'm trying to avoid RefCell
due to the runtime overhead.