Hey there!
I think I am doing something basically wrong here. I am designing a TTL cache that has an internal async updater that listens for stale keys it needs to update. This requires me to have something like:
pub struct Cache<K: 'static + Send, V: 'static + Send> {
data: RwLock<HashMap<K, V>>,
updates_tx: flume::Sender<WriteOp<K, V>>,
}
implementing get
(still logically wrong but I wan the compiler to handle it) is not compiling:
pub fn get<Q>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Hash + Eq,
{
let map = self.data.read();
let res = map.get(k);
res
}
error[E0515]: cannot return value referencing local variable map
Now I understand that the internal map is borrowed by get
but actually, what I want to express here, is that map
is just a temporary value and the real owner is self
. The fact that the map owns its data and that get
should return a reference to the reader is fine.
Maybe something is basically wrong in my design here?