enable class constants in folded classes
[hiphop-php.git] / hphp / hack / src / rupro / lib / cache.rs
blobbccff7559fde852497ae32aef56b2333463ff26c
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 //
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 use dashmap::DashMap;
7 use std::cmp::Eq;
8 use std::fmt::Debug;
9 use std::hash::Hash;
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> {
20     cache: DashMap<K, V>,
23 impl<K: Hash + Eq, V> Default for NonEvictingCache<K, V> {
24     fn default() -> Self {
25         Self {
26             cache: Default::default(),
27         }
28     }
31 impl<K: Hash + Eq, V> NonEvictingCache<K, V> {
32     pub fn new() -> Self {
33         Default::default()
34     }
37 impl<K, V> Cache<K, V> for NonEvictingCache<K, V>
38 where
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))
44     }
46     fn insert(&self, key: K, val: V) {
47         self.cache.insert(key, val);
48     }
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()
54     }