1 // Copyright (c) Meta Platforms, Inc. and affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
11 /// A threadsafe cache, intended for global decl storage. The key type is
12 /// intended to be a `Symbol` or tuple of `Symbol`s, and the value type is
13 /// intended to be a ref-counted pointer (like `Arc` or `Hc`).
14 pub trait Cache<K: Copy, V>: Debug + Send + Sync {
15 fn get(&self, key: K) -> Option<V>;
16 fn insert(&self, key: K, val: V);
19 pub struct NonEvictingCache<K: Hash + Eq, V> {
23 impl<K: Hash + Eq, V> Default for NonEvictingCache<K, V> {
24 fn default() -> Self {
26 cache: Default::default(),
31 impl<K: Hash + Eq, V> NonEvictingCache<K, V> {
32 pub fn new() -> Self {
37 impl<K, V> Cache<K, V> for NonEvictingCache<K, V>
39 K: Copy + Send + Sync + Hash + Eq,
40 V: Clone + Send + Sync,
42 fn get(&self, key: K) -> Option<V> {
43 self.cache.get(&key).map(|x| V::clone(&*x))
46 fn insert(&self, key: K, val: V) {
47 self.cache.insert(key, val);
51 impl<K: Hash + Eq, V> Debug for NonEvictingCache<K, V> {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 f.debug_struct("NonEvictingCache").finish()