inc_complete/storage/mod.rs
1use crate::{Cell, DbHandle};
2
3mod btreemapped;
4mod hashmapped;
5mod macros;
6mod singleton;
7
8pub use btreemapped::BTreeMapStorage;
9pub use hashmapped::HashMapStorage;
10pub use singleton::SingletonStorage;
11
12/// The Storage trait is implemented on a type which can cache all of the computations
13/// used in the program (or a subset of it). These types are typically composed of
14/// several fields with each field implementing `StorageFor<T>` where `T` is one
15/// computation type. Note that each computation type must be unique within a `Storage` type.
16///
17/// This trait is most often automatically implemented by `impl_storage!`, see the documentation
18/// on that macro for usage details.
19///
20/// Note that during serialization, the entire Storage is serialized along with the `Db` object.
21/// To achieve backwards-compatible serialization even when new fields for new computation types
22/// are added, it is recommended to use `#[serde(default)]` on any newly-added fields to still
23/// be able to deserialize from older versions without that field.
24pub trait Storage: Sized {
25 /// For the computation type with the given computation id, return true if the
26 /// output with the given Cell has not yet been set.
27 fn output_is_unset(&self, cell: Cell, computation_id: u32) -> bool;
28
29 /// For the computation type with the given computation id, run the computation
30 /// with the corresponding Cell, returning true if the result changed from its previous value.
31 fn run_computation(db: &mut DbHandle<Self>, cell: Cell, computation_id: u32) -> bool;
32}
33
34/// This trait is implemented by a type storing a single computation type `C`.
35/// Examples include `HashMapStorage<C>`, `SingletonStorage<C>`, and `BTreeMapStorage<C>`.
36///
37/// To implement this efficiently, most types implementing this are two-way maps
38/// from `C` to `Cell` and from `Cell` to `(C, Option<C::Output>)`.
39pub trait StorageFor<C: OutputType> {
40 /// Given a computation key, return the cell associated with it, if it exists.
41 fn get_cell_for_computation(&self, key: &C) -> Option<Cell>;
42
43 /// Insert a new Cell with the given computation that has yet to be run
44 fn insert_new_cell(&mut self, cell: Cell, key: C);
45
46 /// Retrieve the input for this computation.
47 /// The input is expected to already be inserted into this storage.
48 fn get_input(&self, cell: Cell) -> &C;
49
50 /// Retrieve the output for the given cell, if it exists
51 fn get_output(&self, cell: Cell) -> Option<&C::Output>;
52
53 /// `C` has been re-run and has returned the output `new_value`, return `true`
54 /// if `new_value` has changed from its previous value, and cache the new value
55 /// if needed.
56 fn update_output(&mut self, cell: Cell, new_value: C::Output) -> bool;
57}
58
59pub trait ComputationId {
60 fn computation_id() -> u32;
61}
62
63pub trait OutputType {
64 type Output;
65}
66
67pub trait Run<Storage>: OutputType {
68 fn run(&self, db: &mut DbHandle<Storage>) -> Self::Output;
69}