inc_complete/storage/macros.rs
1/// Helper macro to define an intermediate computation type.
2/// This will implement `OutputType`, `ComputationId`, and `Run`.
3///
4/// This macro supports multiple `Storage` types used, separated by `|`,
5/// in case your program uses multiple databases with differing storage types.
6///
7/// Signature:
8/// `define_intermediate!(computation_id, ComputationType -> OutputType, StorageType ( | MoreStorageTypes)*, run_function)`
9///
10/// Example usage:
11/// ```
12/// # use inc_complete::{ define_intermediate, define_input, storage::SingletonStorage, impl_storage, DbHandle };
13/// # struct MyStorageType { input: SingletonStorage<MyInput>, double: SingletonStorage<Double>, more: SingletonStorage<More> }
14/// # #[derive(Clone)]
15/// # struct MyInput;
16/// # define_input!(0, MyInput -> i32, MyStorageType);
17/// # impl_storage!(MyStorageType, input:MyInput, double:Double, more:More,);
18/// ##[derive(Clone)]
19/// struct Double;
20/// ##[derive(Clone)]
21/// struct More;
22///
23/// // Define `Double` as a computation with id 1 and the given run function which returns an `i32`
24/// // to be used with a `Db<MyStorageType>` or `DbHandle<MyStorageType>`.
25/// // The type annotations on the closure are unnecessary.
26/// // We also may provide an existing function instead of a closure.
27/// define_intermediate!(1, Double -> i32, MyStorageType, |_: &Double, db: &DbHandle<MyStorageType>| {
28/// db.get(MyInput) * 2
29/// });
30///
31/// // It is also possible to signal that the value always changes with the assume_changed keyword.
32/// // Doing so let's us avoid expensive `Eq` checks on large values which are expected to change whenever their inputs do anyway:
33/// define_intermediate!(2, assume_changed More -> i32, MyStorageType, |_, db: &DbHandle<MyStorageType>| {
34/// db.get(Double) + 1
35/// });
36/// ```
37#[macro_export]
38macro_rules! define_intermediate {
39 ( $id:tt, $type_name:ident -> $output_type:ty, $( $storage_type:ty )|+, $run_function:expr) => {
40 define_intermediate!(@ $id, $type_name -> $output_type, false, $( $storage_type )|+, $run_function);
41 };
42 ( $id:tt, assume_changed $type_name:ident -> $output_type:ty, $( $storage_type:ty )|+, $run_function:expr) => {
43 define_intermediate!(@ $id, $type_name -> $output_type, true, $( $storage_type )|+, $run_function);
44 };
45 (@ $id:tt, $type_name:ident -> $output_type:ty, $assume_changed:expr, $( $storage_type:ty )|+, $run_function:expr) => {
46 impl $crate::OutputType for $type_name {
47 type Output = $output_type;
48 const IS_INPUT: bool = false;
49 const ASSUME_CHANGED: bool = $assume_changed;
50 }
51
52 impl $crate::ComputationId for $type_name {
53 fn computation_id() -> u32 {
54 $id
55 }
56 }
57
58 impl $type_name {
59 #[allow(unused)]
60 pub fn get(self, db: &impl $crate::DbGet<$type_name>) -> $output_type {
61 db.get(self)
62 }
63 }
64
65 $(
66 impl $crate::Run<$storage_type> for $type_name {
67 fn run(&self, db: &$crate::DbHandle<$storage_type>) -> $output_type {
68 // The type annotation here makes it so that users don't have to annotate
69 // the arguments of `run_function`.
70 let f: fn(&Self, &$crate::DbHandle<$storage_type>) -> $output_type =
71 $run_function;
72 f(self, db)
73 }
74 }
75 )+
76 };
77}
78
79/// Helper macro to define an input computation type.
80/// This will implement `OutputType`, `ComputationId`, and `Run`.
81/// Note that the `Run` implementation will panic by default with a message that
82/// `update_input` should have been called beforehand.
83///
84/// This macro supports multiple `Storage` types used, separated by `|`,
85/// in case your program uses multiple databases with differing storage types.
86///
87/// Signature:
88/// `define_input!(computation_id, ComputationType -> OutputType, StorageType ( | MoreStorageTypes)* )`
89///
90/// Example usage:
91/// ```
92/// # use inc_complete::{ define_intermediate, define_input, storage::SingletonStorage, impl_storage, DbHandle };
93/// # struct MyStorageType { input: SingletonStorage<MyInput>, double: SingletonStorage<Double> }
94/// # impl_storage!(MyStorageType, input:MyInput,double:Double,);
95/// # #[derive(Clone)]
96/// # struct Double;
97/// # define_intermediate!(1, Double -> i32, MyStorageType, |_: &Double, db: &DbHandle<MyStorageType>| {
98/// # db.get(MyInput) * 2
99/// # });
100/// ##[derive(Clone)]
101/// struct MyInput;
102///
103/// // Define `MyInput` as an input computation with id 0 and an `i32` value
104/// // which can be used with a `Db<MyStorageType>`.
105/// define_input!(0, MyInput -> i32, MyStorageType);
106/// ```
107#[macro_export]
108macro_rules! define_input {
109 ( $id:tt, $type_name:ident -> $output_type:ty, $( $storage_type:ty )|+ ) => {
110 define_input!(@ $id, $type_name -> $output_type, false, $( $storage_type )|+);
111 };
112 ( $id:tt, assume_changed $type_name:ident -> $output_type:ty, $( $storage_type:ty )|+ ) => {
113 define_input!(@ $id, $type_name -> $output_type, true, $( $storage_type )|+);
114 };
115 (@ $id:tt, $type_name:ident -> $output_type:ty, $assume_changed:expr, $( $storage_type:ty )|+ ) => {
116 impl $crate::OutputType for $type_name {
117 type Output = $output_type;
118 const IS_INPUT: bool = true;
119 const ASSUME_CHANGED: bool = $assume_changed;
120 }
121
122 impl $crate::ComputationId for $type_name {
123 fn computation_id() -> u32 {
124 $id
125 }
126 }
127
128 impl $type_name {
129 #[allow(unused)]
130 pub fn get(self, db: &impl $crate::DbGet<$type_name>) -> $output_type {
131 db.get(self)
132 }
133
134 #[allow(unused)]
135 pub fn set<S>(self, db: &mut $crate::Db<S>, value: $output_type) where S: $crate::Storage + $crate::StorageFor<$type_name> {
136 db.update_input(self, value);
137 }
138 }
139
140 $(
141 impl $crate::Run<$storage_type> for $type_name {
142 fn run(&self, _: &$crate::DbHandle<$storage_type>) -> $output_type {
143 panic!("Attempted to call `run` function on input {}, did you forget to call `update_input`?",
144 stringify!($type_name))
145 }
146 }
147 )+
148 };
149}
150
151/// Implements `Storage` for a struct type. This enables the given struct type `S` to be used
152/// as a generic on `Db<S>` to store _all_ computations cached by the program.
153///
154/// This will also create forwarding impls for `StorageFor<ComputationType>` for each field,
155/// computation type pair used.
156///
157/// Example usage:
158/// ```
159/// use inc_complete::{ impl_storage, define_input, define_intermediate };
160/// use inc_complete::storage::{ SingletonStorage, HashMapStorage };
161/// use inc_complete::accumulate::{ Accumulator, Accumulate };
162///
163/// ##[derive(Default)]
164/// struct MyStorage {
165/// foos: SingletonStorage<Foo>,
166/// bars: HashMapStorage<Bar>,
167/// logs: Accumulator<Log>,
168/// }
169///
170/// impl_storage!(MyStorage,
171/// foos: Foo,
172/// bars: Bar,
173/// @accumulators {
174/// logs: Log,
175/// }
176/// );
177///
178/// // Each input & intermediate computation should implement Clone
179/// ##[derive(Clone)]
180/// struct Foo;
181/// define_input!(0, Foo -> usize, MyStorage);
182///
183/// // HashMapStorage requires Eq and Hash
184/// ##[derive(Clone, PartialEq, Eq, Hash)]
185/// struct Bar(std::rc::Rc<String>);
186/// define_intermediate!(1, Bar -> usize, MyStorage, |bar, db| {
187/// bar.0.len() + db.get(Foo)
188/// });
189///
190/// // Accumulated values need to derive Eq and Clone
191/// #[derive(PartialEq, Eq, Clone)]
192/// struct Log;
193/// ```
194///
195/// Note that using this macro requires each computation type to implement `Clone`.
196#[macro_export]
197macro_rules! impl_storage {
198 ($typ:ty, $( $field:ident : $computation_type:ty, )* $(@accumulators { $($acc_field:ident : $acc_type:ty, )* })? ) => {
199 impl $crate::Storage for $typ {
200 fn output_is_unset(&self, cell: $crate::Cell, computation_id: u32) -> bool {
201 use $crate::StorageFor;
202 match computation_id {
203 $(
204 x if x == <$computation_type as $crate::ComputationId>::computation_id() => {
205 self.$field.get_output(cell).is_none()
206 },
207 )*
208 id => panic!("Unknown computation id: {id}"),
209 }
210 }
211 $crate::run_computation!( $($field: $computation_type),* );
212
213 fn gc(&mut self, used_cells: &std::collections::HashSet<$crate::Cell>) {
214 use $crate::StorageFor;
215 $(
216 self.$field.gc(&used_cells);
217 )*
218 }
219 }
220
221 $(
222 impl $crate::StorageFor<$computation_type> for $typ {
223 fn get_cell_for_computation(&self, key: &$computation_type) -> Option<$crate::Cell> {
224 self.$field.get_cell_for_computation(key)
225 }
226
227 fn insert_new_cell(&self, cell: $crate::Cell, key: $computation_type) {
228 self.$field.insert_new_cell(cell, key)
229 }
230
231 fn get_input(&self, cell: $crate::Cell) -> $computation_type {
232 self.$field.get_input(cell)
233 }
234
235 fn get_output(&self, cell: $crate::Cell) -> Option<<$computation_type as $crate::OutputType>::Output> {
236 self.$field.get_output(cell)
237 }
238
239 fn update_output(&self, cell: $crate::Cell, new_value: <$computation_type as $crate::OutputType>::Output) -> bool {
240 self.$field.update_output(cell, new_value)
241 }
242
243 fn gc(&mut self, used_cells: &std::collections::HashSet<$crate::Cell>) {
244 self.$field.gc(used_cells);
245 }
246 })*
247
248 $($(
249 impl $crate::accumulate::Accumulate<$acc_type> for $typ {
250 fn accumulate(&self, cell: $crate::Cell, item: $acc_type) {
251 self.$acc_field.accumulate(cell, item)
252 }
253
254 fn get_accumulated<Items>(&self, cells: &[$crate::Cell]) -> Items
255 where Items: FromIterator<$acc_type>
256 {
257 self.$acc_field.get_accumulated(cells)
258 }
259 }
260 )*)?
261 };
262}
263
264#[doc(hidden)]
265#[macro_export]
266macro_rules! run_computation {
267 ( $($field:ident: $computation_type:ty),* ) => {
268 fn run_computation(db: &$crate::DbHandle<Self>, cell: $crate::Cell, computation_id: u32) -> bool {
269 use $crate::{ StorageFor, Run };
270 match computation_id {
271 $(
272 x if x == <$computation_type as $crate::ComputationId>::computation_id() => {
273 let new_value = db.storage().$field.get_input(cell).run(db);
274 db.storage().$field.update_output(cell, new_value)
275 }
276 )*
277 id => panic!("Unknown computation id: {id}"),
278 }
279 }
280 }
281}