use super::CacheTrait;
#[derive(Default)]
pub struct CacheStorage {
caches: ahash::HashMap<core::any::TypeId, Box<dyn CacheTrait>>,
}
impl CacheStorage {
pub fn cache<Cache: CacheTrait + Default>(&mut self) -> &mut Cache {
let cache = self
.caches
.entry(core::any::TypeId::of::<Cache>())
.or_insert_with(|| Box::<Cache>::default());
#[expect(clippy::unwrap_used)]
(cache.as_mut() as &mut dyn core::any::Any)
.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 core::fmt::Debug for CacheStorage {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"FrameCacheStorage[{} caches with {} elements]",
self.caches.len(),
self.num_values()
)
}
}