summaryrefslogtreecommitdiffstats
path: root/graphics/src/cached.rs
diff options
context:
space:
mode:
Diffstat (limited to 'graphics/src/cached.rs')
-rw-r--r--graphics/src/cached.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/graphics/src/cached.rs b/graphics/src/cached.rs
new file mode 100644
index 00000000..b52f9d9d
--- /dev/null
+++ b/graphics/src/cached.rs
@@ -0,0 +1,42 @@
+use crate::Primitive;
+
+use std::sync::Arc;
+
+/// A piece of data that can be cached.
+pub trait Cached: Sized {
+ /// The type of cache produced.
+ type Cache;
+
+ /// Loads the [`Cache`] into a proper instance.
+ ///
+ /// [`Cache`]: Self::Cache
+ fn load(cache: &Self::Cache) -> Self;
+
+ /// Caches this value, producing its corresponding [`Cache`].
+ ///
+ /// [`Cache`]: Self::Cache
+ fn cache(self) -> Self::Cache;
+}
+
+impl<T> Cached for Primitive<T> {
+ type Cache = Arc<Self>;
+
+ fn load(cache: &Arc<Self>) -> Self {
+ Self::Cache {
+ content: cache.clone(),
+ }
+ }
+
+ fn cache(self) -> Arc<Self> {
+ Arc::new(self)
+ }
+}
+
+#[cfg(debug_assertions)]
+impl Cached for () {
+ type Cache = ();
+
+ fn load(_cache: &Self::Cache) -> Self {}
+
+ fn cache(self) -> Self::Cache {}
+}