summaryrefslogtreecommitdiffstats
path: root/graphics/src/cached.rs
blob: 1ba82f9f29e09e1db81cd4238a4d1426978b90e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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: Clone;

    /// 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, previous: Option<Self::Cache>) -> 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, _previous: Option<Arc<Self>>) -> Arc<Self> {
        Arc::new(self)
    }
}

#[cfg(debug_assertions)]
impl Cached for () {
    type Cache = ();

    fn load(_cache: &Self::Cache) -> Self {}

    fn cache(self, _previous: Option<Self::Cache>) -> Self::Cache {}
}