summaryrefslogtreecommitdiffstats
path: root/graphics/src/cached.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-03-25 21:36:44 +0100
committerLibravatar GitHub <noreply@github.com>2024-03-25 21:36:44 +0100
commita2a8381a49ac2dd1cd65eb382b9ee02bbfa17286 (patch)
treee6c24928a42e23ff91eea0fc30b4fbbcb6da024b /graphics/src/cached.rs
parent3013463baa71504488a20436beb3db87ecb66df0 (diff)
parent6a4f5ac2081699f7cf20c917b367366ab49eeef1 (diff)
downloadiced-a2a8381a49ac2dd1cd65eb382b9ee02bbfa17286.tar.gz
iced-a2a8381a49ac2dd1cd65eb382b9ee02bbfa17286.tar.bz2
iced-a2a8381a49ac2dd1cd65eb382b9ee02bbfa17286.zip
Merge pull request #2351 from iced-rs/custom-renderer-injection
Type-Driven Renderer Fallback
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 {}
+}