#[cfg(not(feature = "mock_data"))] use crate::ExternRef; pub trait Cacheable { #[cfg(not(feature = "mock_data"))] fn get_cache<'a>() -> &'a mut hashbrown::HashMap, Self> where Self: Sized; #[cfg(not(feature = "mock_data"))] fn from_ref(ptr: ExternRef, ctor: &dyn Fn(ExternRef) -> Self) -> Self where Self: Sized, Self: Clone, { let opt = Self::get_cache().get(&ptr); if let Some(v) = opt { return v.clone(); } let value = ctor(ptr); Self::get_cache().insert(ptr, value.clone()); value } } macro_rules! cacheable { ($type: ty) => { paste::paste! { #[cfg(not(feature = "mock_data"))] static mut [<$type:upper _CACHE>]: Option, $type>> = None; impl crate::handling::Cacheable for $type { #[cfg(not(feature = "mock_data"))] fn get_cache<'a>() -> &'a mut hashbrown::HashMap, $type> { unsafe { if let None = [<$type:upper _CACHE>] { [<$type:upper _CACHE>] = Some(hashbrown::HashMap::new()); } [<$type:upper _CACHE>].as_mut().unwrap() } } } } }; } pub(crate) use cacheable;