inc_complete/storage/mod.rs
1use crate::{Cell, DbHandle};
2
3mod hashmapped;
4mod indexmapped;
5mod macros;
6mod singleton;
7
8pub use hashmapped::HashMapStorage;
9pub use indexmapped::TreeIndexStorage;
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 #[cfg(not(feature = "async"))]
32 fn run_computation(db: &DbHandle<Self>, cell: Cell, computation_id: u32) -> bool;
33
34 /// For the computation type with the given computation id, run the computation
35 /// with the corresponding Cell, returning true if the result changed from its previous value.
36 #[cfg(feature = "async")]
37 fn run_computation<'db>(
38 db: &DbHandle<'db, Self>,
39 cell: Cell,
40 computation_id: u32,
41 ) -> impl Future<Output = bool> + Send
42 where
43 Self: Sync;
44}
45
46/// This trait is implemented by a type storing a single computation type `C`.
47/// Examples include `HashMapStorage<C>`, `SingletonStorage<C>`, and `BTreeMapStorage<C>`.
48///
49/// To implement this efficiently, most types implementing this are two-way maps
50/// from `C` to `Cell` and from `Cell` to `(C, Option<C::Output>)`.
51pub trait StorageFor<C: OutputType> {
52 /// Given a computation key, return the cell associated with it, if it exists.
53 fn get_cell_for_computation(&self, key: &C) -> Option<Cell>;
54
55 /// Insert a new Cell with the given computation that has yet to be run
56 fn insert_new_cell(&self, cell: Cell, key: C);
57
58 /// Retrieve the input for this computation.
59 /// The input is expected to already be inserted into this storage.
60 fn get_input(&self, cell: Cell) -> C;
61
62 /// Retrieve the output for the given cell, if it exists
63 fn get_output(&self, cell: Cell) -> Option<C::Output>;
64
65 /// `C` has been re-run and has returned the output `new_value`, return `true`
66 /// if `new_value` has changed from its previous value, and cache the new value
67 /// if needed.
68 fn update_output(&self, cell: Cell, new_value: C::Output) -> bool;
69}
70
71pub trait ComputationId {
72 fn computation_id() -> u32;
73}
74
75pub trait OutputType {
76 type Output;
77 const IS_INPUT: bool;
78}
79
80pub trait Run<Storage>: OutputType {
81 #[cfg(not(feature = "async"))]
82 fn run(&self, db: &DbHandle<Storage>) -> Self::Output;
83
84 #[cfg(feature = "async")]
85 fn run<'db>(&self, db: &DbHandle<'db, Storage>) -> impl Future<Output = Self::Output>
86 where
87 Storage: Sync;
88}