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(RefCell>>); impl Default for CacheSet { fn default() -> Self { Self::new() } } impl CacheSet { pub fn new() -> Self { Self(RefCell::new(Set::new())) } } impl Clone for CacheSet { fn clone(&self) -> Self { Self(self.0.clone()) } } impl Clone for CacheSet { fn clone(&self) -> Self { Self(self.0.clone()) } } impl CacheSet { 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 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 CacheSet { pub fn get(&self, q: &Q) -> Rc where Rc: Borrow, Q: ToOwned, { 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(RefCell>>); pub struct GlobalCacheDict(LocalKey>>);