swc_common/
cache.rs

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
use std::ops::Deref;

use once_cell::sync::OnceCell;

/// Wrapper for [OnceCell] with support for [rkyv].
#[derive(Clone, Debug)]
pub struct CacheCell<T>(OnceCell<T>);

impl<T> Deref for CacheCell<T> {
    type Target = OnceCell<T>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> CacheCell<T> {
    pub fn new() -> Self {
        Self(OnceCell::new())
    }
}

impl<T> From<T> for CacheCell<T> {
    fn from(value: T) -> Self {
        Self(OnceCell::from(value))
    }
}

impl<T> Default for CacheCell<T> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(feature = "rkyv-impl")]
mod rkyv_impl {
    use std::hint::unreachable_unchecked;

    use rancor::Fallible;
    use rkyv::{
        munge::munge, option::ArchivedOption, traits::NoUndef, Archive, Deserialize, Place,
        Serialize,
    };

    use super::*;

    #[allow(dead_code)]
    #[repr(u8)]
    enum ArchivedOptionTag {
        None,
        Some,
    }

    // SAFETY: `ArchivedOptionTag` is `repr(u8)` and so always consists of a single
    // well-defined byte.
    unsafe impl NoUndef for ArchivedOptionTag {}

    #[repr(C)]
    struct ArchivedOptionVariantNone(ArchivedOptionTag);

    #[repr(C)]
    struct ArchivedOptionVariantSome<T>(ArchivedOptionTag, T);

    impl<T: Archive> Archive for CacheCell<T> {
        type Archived = ArchivedOption<T::Archived>;
        type Resolver = Option<T::Resolver>;

        fn resolve(&self, resolver: Self::Resolver, out: Place<Self::Archived>) {
            match resolver {
                None => {
                    let out = unsafe { out.cast_unchecked::<ArchivedOptionVariantNone>() };
                    munge!(let ArchivedOptionVariantNone(tag) = out);
                    tag.write(ArchivedOptionTag::None);
                }
                Some(resolver) => {
                    let out =
                        unsafe { out.cast_unchecked::<ArchivedOptionVariantSome<T::Archived>>() };
                    munge!(let ArchivedOptionVariantSome(tag, out_value) = out);
                    tag.write(ArchivedOptionTag::Some);

                    let value = if let Some(value) = self.get() {
                        value
                    } else {
                        unsafe {
                            unreachable_unchecked();
                        }
                    };

                    value.resolve(resolver, out_value);
                }
            }
        }
    }

    impl<T: Serialize<S>, S: Fallible + ?Sized> Serialize<S> for CacheCell<T> {
        fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
            self.get()
                .map(|value| value.serialize(serializer))
                .transpose()
        }
    }

    impl<T, D> Deserialize<CacheCell<T>, D> for ArchivedOption<T::Archived>
    where
        T: Archive,
        T::Archived: Deserialize<T, D>,
        D: Fallible + ?Sized,
    {
        fn deserialize(&self, deserializer: &mut D) -> Result<CacheCell<T>, D::Error> {
            Ok(match self {
                ArchivedOption::Some(value) => CacheCell::from(value.deserialize(deserializer)?),
                ArchivedOption::None => CacheCell::new(),
            })
        }
    }
}