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
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::borrow::{Borrow, ToOwned};
use std::cell::RefCell;
use std::hash::Hash;
use std::rc::Rc;
use std::thread::LocalKey;

use crate::dict::Dict;
use crate::set::Set;
use crate::{RcArray, Str};

#[derive(Debug)]
pub struct CacheSet<T: ?Sized>(RefCell<Set<Rc<T>>>);

impl<T: ?Sized> Default for CacheSet<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T: ?Sized> CacheSet<T> {
    pub fn new() -> Self {
        Self(RefCell::new(Set::new()))
    }
}

impl Clone for CacheSet<str> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T: Hash + Eq> Clone for CacheSet<T> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl CacheSet<str> {
    pub fn get(&self, s: &str) -> Str {
        if let Some(cached) = self.0.borrow().get(s) {
            return cached.clone().into();
        } // &self.0 is dropped
        let s = Str::rc(s);
        self.0.borrow_mut().insert(s.clone().into_rc());
        s
    }
}

impl<T: Hash + Eq + Clone> CacheSet<[T]> {
    pub fn get(&self, q: &[T]) -> Rc<[T]> {
        if let Some(cached) = self.0.borrow().get(q) {
            return cached.clone();
        } // &self.0 is dropped
        let s = RcArray::from(q);
        self.0.borrow_mut().insert(s.clone());
        s
    }
}

impl<T: Hash + Eq> CacheSet<T> {
    pub fn get<Q: ?Sized + Hash + Eq>(&self, q: &Q) -> Rc<T>
    where
        Rc<T>: Borrow<Q>,
        Q: ToOwned<Owned = T>,
    {
        if let Some(cached) = self.0.borrow().get(q) {
            return cached.clone();
        } // &self.0 is dropped
        let s = Rc::from(q.to_owned());
        self.0.borrow_mut().insert(s.clone());
        s
    }
}

pub struct CacheDict<K, V: ?Sized>(RefCell<Dict<K, Rc<V>>>);

pub struct GlobalCacheDict<K: 'static, V: ?Sized + 'static>(LocalKey<RefCell<CacheDict<K, V>>>);