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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use std::borrow::Cow;

use scoped_tls::scoped_thread_local;

scoped_thread_local!(pub static ALLOW_DEEP_CLONE: ());

pub trait AssertCloneCheap {
    /// Assert that `self` is cheap to clone. This noop on production build.
    fn assert_clone_cheap(&self);
}

impl<T> AssertCloneCheap for T
where
    T: Freeze,
{
    fn assert_clone_cheap(&self) {
        #[cfg(debug_assertions)]
        if !self.is_clone_cheap() {
            unreachable!("`{}` is not cheap to clone", std::any::type_name::<T>())
        }
    }
}

pub trait Freeze: Sized + Clone {
    /// Returns `true` if `[Clone::clone] is cheap.
    fn is_clone_cheap(&self) -> bool;

    /// Make `[Clone::clone] cheap.
    fn freeze(&mut self);

    /// Call [`Self::freeze`] and return `self`.
    #[inline]
    fn freezed(mut self) -> Self {
        self.freeze();
        self
    }
}

impl<T> Freeze for Option<T>
where
    T: Freeze,
{
    fn is_clone_cheap(&self) -> bool {
        match self {
            Some(v) => v.is_clone_cheap(),
            None => true,
        }
    }

    fn freeze(&mut self) {
        match self {
            Some(v) => v.freeze(),
            None => {}
        }
    }
}

impl<T> Freeze for Vec<T>
where
    T: Freeze,
{
    fn is_clone_cheap(&self) -> bool {
        self.iter().all(|v| v.is_clone_cheap())
    }

    fn freeze(&mut self) {
        self.iter_mut().for_each(|v| v.freeze())
    }
}

impl<T> Freeze for Box<T>
where
    T: Freeze,
{
    fn freeze(&mut self) {
        (**self).freeze()
    }

    fn is_clone_cheap(&self) -> bool {
        (**self).is_clone_cheap()
    }
}

/// TODO(kdy1): This can be confusing.
impl<T> Freeze for Cow<'_, T>
where
    T: Clone + Freeze,
{
    fn freeze(&mut self) {
        match self {
            Cow::Borrowed(v) => {
                if !v.is_clone_cheap() {
                    let mut v = ALLOW_DEEP_CLONE.set(&(), || v.clone());
                    v.freeze();
                    *self = Cow::Owned(v);
                }
            }
            Cow::Owned(v) => {
                v.freeze();
            }
        }
    }

    fn is_clone_cheap(&self) -> bool {
        (**self).is_clone_cheap()
    }
}

#[macro_export]
macro_rules! try_cache {
    ($cache:expr, $key:expr, $default_op:expr) => {{
        let key = $key;

        let cached = if $cache.can_cache(&key) { $cache.get(&key) } else { None };

        if let Some(v) = cached {
            v
        } else {
            let v: Result<_, _> = (|| $default_op)();
            let v = match v {
                Ok(v) => v,
                Err(err) => return Err(err),
            };
            let v = $cache.insert(key, v);

            v
        }
    }};
}