Fix eviction in MonoCache::find_or_create

This commit is contained in:
Keegan McAllister 2013-10-11 13:44:48 -07:00
parent fc9fdf30a6
commit 5289d35145

View file

@ -34,13 +34,13 @@ impl<K: Clone + Eq, V: Clone> Cache<K,V> for MonoCache<K,V> {
} }
fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V { fn find_or_create(&mut self, key: &K, blk: &fn(&K) -> V) -> V {
match self.entry { match self.find(key) {
None => { Some(value) => value,
None => {
let value = blk(key); let value = blk(key);
self.entry = Some((key.clone(), value.clone())); self.entry = Some((key.clone(), value.clone()));
value value
}, }
Some((ref _k, ref v)) => v.clone()
} }
} }
@ -58,11 +58,9 @@ fn test_monocache() {
assert!(cache.find(&1).is_some()); assert!(cache.find(&1).is_some());
assert!(cache.find(&2).is_none()); assert!(cache.find(&2).is_none());
/* FIXME: clarify behavior here:
cache.find_or_create(&2, |_v| { two }); cache.find_or_create(&2, |_v| { two });
assert!(cache.find(&2).is_some()); assert!(cache.find(&2).is_some());
assert!(cache.find(&1).is_none()); assert!(cache.find(&1).is_none());
*/
} }
pub struct HashCache<K, V> { pub struct HashCache<K, V> {