49 lines
1.4 KiB
Rust
Executable File
49 lines
1.4 KiB
Rust
Executable File
#[cfg(not(feature = "mock_data"))]
|
|
use crate::ExternRef;
|
|
|
|
pub trait Cacheable {
|
|
#[cfg(not(feature = "mock_data"))]
|
|
fn get_cache<'a>() -> &'a mut hashbrown::HashMap<ExternRef<Self>, Self>
|
|
where
|
|
Self: Sized;
|
|
|
|
#[cfg(not(feature = "mock_data"))]
|
|
fn from_ref(ptr: ExternRef<Self>, ctor: &dyn Fn(ExternRef<Self>) -> 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<hashbrown::HashMap<ExternRef<$type>, $type>> =
|
|
None;
|
|
impl crate::handling::Cacheable for $type {
|
|
#[cfg(not(feature = "mock_data"))]
|
|
fn get_cache<'a>() -> &'a mut hashbrown::HashMap<ExternRef<$type>, $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;
|