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        self.key
42            .set(key)
43            .unwrap_or_else(|_| panic!("insert_new_cell: cell already initialized"));
44    }
45
46    fn try_get_input(&self, cell: Cell) -> Option<K> {
47        if cell == self.cell.get().cloned()? {
48            self.key.get().cloned()
49        } else {
50            None
51        }
52    }
53
54    fn get_input(&self, _: Cell) -> K {
55        self.key.get().cloned().unwrap()
56    }
57
58    fn get_output(&self, _: Cell) -> Option<K::Output> {
59        self.value.lock().unwrap().clone()
60    }
61
62    fn update_output(&self, _: Cell, new_value: K::Output) -> bool {
63        let mut guard = self.value.lock().unwrap();
64        let changed = K::ASSUME_CHANGED || guard.as_ref().is_none_or(|value| *value != new_value);
65        *guard = Some(new_value);
66        changed
67    }
68
69    fn gc(&mut self, used_cells: &std::collections::HashSet<Cell>) {
70        if let Some(this_cell) = self.cell.get() {
71            if !used_cells.contains(this_cell) {
72                if let Ok(val) = self.value.get_mut() {
73                    *val = None;
74                }
75            }
76        }
77    }
78}
79
80impl<K> serde::Serialize for SingletonStorage<K>
81where
82    K: serde::Serialize + Computation,
83    K::Output: serde::Serialize,
84{
85    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
86    where
87        S: serde::Serializer,
88    {
89        let mut s = serializer.serialize_struct("SingletonStorage", 3)?;
90        if let Some(cell) = self.cell.get() {
91            s.serialize_field("cell", cell)?;
92        }
93        if let Some(key) = self.key.get() {
94            s.serialize_field("key", key)?;
95        }
96        if let Ok(lock) = self.value.lock() {
97            if let Some(value) = &*lock {
98                s.serialize_field("value", value)?;
99            }
100        }
101        s.end()
102    }
103}
104
105impl<'de, K> serde::Deserialize<'de> for SingletonStorage<K>
106where
107    K: serde::Deserialize<'de> + Computation,
108    K::Output: serde::Deserialize<'de>,
109{
110    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
111    where
112        D: serde::Deserializer<'de>,
113    {
114        let wrapper: SerializeWrapper<K> = Deserialize::deserialize(deserializer)?;
115        Ok(wrapper.into_storage())
116    }
117}
118
119#[derive(Deserialize)]
120struct SerializeWrapper<K: Computation> {
121    #[serde(default)]
122    cell: Option<Cell>,
123
124    // Serde complains we need a `K: Default` without this, but that shouldn't be necessary.
125    #[serde(default = "none")]
126    key: Option<K>,
127
128    #[serde(default)]
129    #[serde(bound = "K::Output: Deserialize<'de>")]
130    value: Option<K::Output>,
131}
132
133fn none<T>() -> Option<T> {
134    None
135}
136
137impl<K: Computation> SerializeWrapper<K> {
138    fn into_storage(self) -> SingletonStorage<K> {
139        let cell = match self.cell {
140            Some(cell) => std::sync::OnceLock::from(cell),
141            None => std::sync::OnceLock::new(),
142        };
143        let key = match self.key {
144            Some(key) => std::sync::OnceLock::from(key),
145            None => std::sync::OnceLock::new(),
146        };
147        let value = std::sync::Mutex::new(self.value);
148        SingletonStorage { cell, key, value }
149    }
150}