Skip to main content

swc_common/
cache.rs

1use std::ops::Deref;
2
3use once_cell::sync::OnceCell;
4
5/// Wrapper for [OnceCell].
6#[derive(Clone, Debug)]
7pub struct CacheCell<T>(OnceCell<T>);
8
9impl<T> Deref for CacheCell<T> {
10    type Target = OnceCell<T>;
11
12    fn deref(&self) -> &Self::Target {
13        &self.0
14    }
15}
16
17impl<T> CacheCell<T> {
18    pub fn new() -> Self {
19        Self(OnceCell::new())
20    }
21}
22
23impl<T> From<T> for CacheCell<T> {
24    fn from(value: T) -> Self {
25        Self(OnceCell::from(value))
26    }
27}
28
29impl<T> Default for CacheCell<T> {
30    fn default() -> Self {
31        Self::new()
32    }
33}