1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::borrow::Borrow;
use std::cell::RefCell;
use std::hash::Hash;
use std::thread::LocalKey;
use erg_common::dict::Dict;
use erg_type::Type;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SubtypePair {
pub sub: Type,
pub sup: Type,
}
impl SubtypePair {
pub const fn new(sub: Type, sup: Type) -> Self {
Self { sub, sup }
}
}
#[derive(Debug, Default)]
pub struct TypeCmpCache {
cache: Dict<SubtypePair, bool>,
}
impl TypeCmpCache {
pub fn new() -> Self {
Self::default()
}
pub fn get<Q: Eq + Hash>(&self, pair: &Q) -> Option<bool>
where
SubtypePair: Borrow<Q>,
{
self.cache.get(pair).copied()
}
pub fn register(&mut self, pair: SubtypePair, b: bool) {
self.cache.insert(pair, b);
}
}
thread_local! {
static TYPE_CACHE: RefCell<TypeCmpCache> = RefCell::new(TypeCmpCache::default());
}
#[derive(Debug)]
pub struct GlobalTypeCmpCache(LocalKey<RefCell<TypeCmpCache>>);
pub static GLOBAL_TYPE_CACHE: GlobalTypeCmpCache = GlobalTypeCmpCache(TYPE_CACHE);
impl GlobalTypeCmpCache {
pub fn get<Q: Eq + Hash>(&'static self, pair: &Q) -> Option<bool>
where
SubtypePair: Borrow<Q>,
{
self.0.with(|s| s.borrow().get(pair))
}
pub fn register(&'static self, pair: SubtypePair, b: bool) {
self.0.with(|s| s.borrow_mut().register(pair, b));
}
}