Skip to main content

inc_complete/storage/
singleton.rs

1use serde::{Deserialize, ser::SerializeStruct};
2
3use super::{Computation, StorageFor};
4use crate::Cell;
5
6/// Helper to store a simple computation type which has no fields and thus
7/// does not require a map to cache each possible value.
8///
9/// Examples include `struct SourceFile;` or `struct Time;`
10pub struct SingletonStorage<K: Computation> {
11    cell: std::sync::OnceLock<Cell>,
12    key: std::sync::OnceLock<K>,
13    value: std::sync::Mutex<Option<K::Output>>,
14}
15
16impl<K: Computation> Default for SingletonStorage<K> {
17    fn default() -> Self {
18        Self {
19            cell: Default::default(),
20            value: Default::default(),
21            key: Default::default(),
22        }
23    }
24}
25
26impl<K> StorageFor<K> for SingletonStorage<K>
27where
28    K: Computation + Clone,
29    K::Output: Eq + Clone,
30{
31    fn get_cell_for_computation(&self, _: &K) -> Option<Cell> {
32        self.cell.get().copied()
33    }
34
35    fn insert_new_cell(&self, cell: Cell, key: K) {
36        assert!(
37            self.cell.set(cell).is_ok(),
38            "Overwriting previous singleton value - are you using SingleStorage<{}> with a non-singleton type?",
39            std::any::type_name::<K>()
40        );
41        let result = self.key.set(key);
42        result.unwrap_or_else(|_| panic!("insert_new_cell: cell already initialized"));
43    }
44
45    fn try_get_input(&self, cell: Cell) -> Option<K> {
46        if cell == self.cell.get().cloned()? {
47            self.key.get().cloned()
48        } else {
49            None
50        }
51    }
52
53    fn get_input(&self, _: Cell) -> K {
54        self.key.get().cloned().unwrap()
55    }
56
57    fn get_output(&self, _: Cell) -> Option<K::Output> {
58        self.value.lock().unwrap().clone()
59    }
60
61    fn update_output(&self, _: Cell, new_value: K::Output) -> bool {
62        let mut guard = self.value.lock().unwrap();
63        let changed = K::ASSUME_CHANGED || guard.as_ref().is_none_or(|value| *value != new_value);
64        *guard = Some(new_value);
65        changed
66    }
67
68    fn gc(&mut self, used_cells: &std::collections::HashSet<Cell>) {
69        if let Some(this_cell) = self.cell.get() {
70            if !used_cells.contains(this_cell) {
71                if let Ok(val) = self.value.get_mut() {
72                    *val = None;
73                }
74            }
75        }
76    }
77}
78
79impl<K> serde::Serialize for SingletonStorage<K>
80where
81    K: serde::Serialize + Computation,
82    K::Output: serde::Serialize,
83{
84    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
85    where
86        S: serde::Serializer,
87    {
88        let mut s = serializer.serialize_struct("SingletonStorage", 3)?;
89        if let Some(cell) = self.cell.get() {
90            s.serialize_field("cell", cell)?;
91        }
92        if let Some(key) = self.key.get() {
93            s.serialize_field("key", key)?;
94        }
95        if let Ok(lock) = self.value.lock() {
96            if let Some(value) = &*lock {
97                s.serialize_field("value", value)?;
98            }
99        }
100        s.end()
101    }
102}
103
104impl<'de, K> serde::Deserialize<'de> for SingletonStorage<K>
105where
106    K: serde::Deserialize<'de> + Computation,
107    K::Output: serde::Deserialize<'de>,
108{
109    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
110    where
111        D: serde::Deserializer<'de>,
112    {
113        let wrapper: SerializeWrapper<K> = Deserialize::deserialize(deserializer)?;
114        Ok(wrapper.into_storage())
115    }
116}
117
118#[derive(Deserialize)]
119struct SerializeWrapper<K: Computation> {
120    #[serde(default)]
121    cell: Option<Cell>,
122
123    // Serde complains we need a `K: Default` without this, but that shouldn't be necessary.
124    #[serde(default = "none")]
125    key: Option<K>,
126
127    #[serde(default)]
128    #[serde(bound = "K::Output: Deserialize<'de>")]
129    value: Option<K::Output>,
130}
131
132fn none<T>() -> Option<T> {
133    None
134}
135
136impl<K: Computation> SerializeWrapper<K> {
137    fn into_storage(self) -> SingletonStorage<K> {
138        let cell = match self.cell {
139            Some(cell) => std::sync::OnceLock::from(cell),
140            None => std::sync::OnceLock::new(),
141        };
142        let key = match self.key {
143            Some(key) => std::sync::OnceLock::from(key),
144            None => std::sync::OnceLock::new(),
145        };
146        let value = std::sync::Mutex::new(self.value);
147        SingletonStorage { cell, key, value }
148    }
149}