-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
egui::util::cache
to egui::cache
, and improve docstrings
- Loading branch information
Showing
8 changed files
with
108 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use super::CacheTrait; | ||
|
||
/// A typemap of many caches, all implemented with [`CacheTrait`]. | ||
/// | ||
/// You can access egui's caches via [`crate::Memory::caches`], | ||
/// found with [`crate::Context::memory_mut`]. | ||
/// | ||
/// ``` | ||
/// use egui::cache::{CacheStorage, ComputerMut, FrameCache}; | ||
/// | ||
/// #[derive(Default)] | ||
/// struct CharCounter {} | ||
/// impl ComputerMut<&str, usize> for CharCounter { | ||
/// fn compute(&mut self, s: &str) -> usize { | ||
/// s.chars().count() | ||
/// } | ||
/// } | ||
/// type CharCountCache<'a> = FrameCache<usize, CharCounter>; | ||
/// | ||
/// # let mut cache_storage = CacheStorage::default(); | ||
/// let mut cache = cache_storage.cache::<CharCountCache<'_>>(); | ||
/// assert_eq!(cache.get("hello"), 5); | ||
/// ``` | ||
#[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() | ||
} | ||
|
||
/// Total number of cached values | ||
fn num_values(&self) -> usize { | ||
self.caches.values().map(|cache| cache.len()).sum() | ||
} | ||
|
||
/// Call once per frame to evict cache. | ||
pub fn update(&mut self) { | ||
for cache in self.caches.values_mut() { | ||
cache.update(); | ||
} | ||
} | ||
} | ||
|
||
impl Clone for CacheStorage { | ||
fn clone(&self) -> Self { | ||
// We return an empty cache that can be filled in again. | ||
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() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// A cache, storing some value for some length of time. | ||
#[allow(clippy::len_without_is_empty)] | ||
pub trait CacheTrait: 'static + Send + Sync { | ||
/// Call once per frame to evict cache. | ||
fn update(&mut self); | ||
|
||
/// Number of values currently in the cache. | ||
fn len(&self) -> usize; | ||
|
||
fn as_any_mut(&mut self) -> &mut dyn std::any::Any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//! Caches for preventing the same value from being recomputed every frame. | ||
//! | ||
//! Computing the same thing each frame can be expensive, | ||
//! so often you want to save the result from the previous frame and reuse it. | ||
//! | ||
//! Enter [`FrameCache`]: it caches the results of a computation for one frame. | ||
//! If it is still used next frame, it is not recomputed. | ||
//! If it is not used next frame, it is evicted from the cache to save memory. | ||
//! | ||
//! You can access egui's caches via [`crate::Memory::caches`], | ||
//! found with [`crate::Context::memory_mut`]. | ||
mod cache_storage; | ||
mod cache_trait; | ||
mod frame_cache; | ||
|
||
pub use cache_storage::CacheStorage; | ||
pub use cache_trait::CacheTrait; | ||
pub use frame_cache::{ComputerMut, FrameCache}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters