use super::CacheTrait;
#[derive(Default)]
pub struct CacheStorage {
caches: ahash::HashMap<std::any::TypeId, Box<dyn CacheTrait>>,
}
impl CacheStorage {
pub fn cache<Cache: CacheTrait + Default>(&mut self) -> &mut Cache {
self.caches
.entry(std::any::TypeId::of::<Cache>())
.or_insert_with(|| Box::<Cache>::default())
.as_any_mut()
.downcast_mut::<Cache>()
.unwrap()
}
fn num_values(&self) -> usize {
self.caches.values().map(|cache| cache.len()).sum()
}
pub fn update(&mut self) {
self.caches.retain(|_, cache| {
cache.update();
cache.len() > 0
});
}
}
impl Clone for CacheStorage {
fn clone(&self) -> Self {
Self::default()
}
}
impl std::fmt::Debug for CacheStorage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"FrameCacheStorage[{} caches with {} elements]",
self.caches.len(),
self.num_values()
)
}
}