inc_complete/computation/
input.rs

1use crate::{Cell, DbHandle};
2
3use super::Computation;
4
5/// Helper to define a Computation for an input type which has no dependencies
6/// and thus requires an explicit update from `db.update_input` to be initialized
7/// instead of a `run` function.
8///
9/// When using an input in a Computation tuple, you still need to decide on a storage
10/// type wrapper. An example would be `HashMapStorage<Input<T>>`.
11///
12/// Examples include `struct FileContents { text: String };` or `struct Time;`
13#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
14pub struct Input<T>(T);
15
16impl<T> Input<T> {
17    pub const fn new(value: T) -> Self {
18        Self(value)
19    }
20}
21
22pub trait OutputTypeForInput: Clone {
23    type Output: Eq;
24}
25
26impl<T: OutputTypeForInput + 'static> Computation for Input<T> {
27    type Output = <T as OutputTypeForInput>::Output;
28    type Storage = (Option<Cell>, Option<Self::Output>);
29
30    fn run(&self, _: &mut DbHandle<impl Computation>) -> Self::Output {
31        panic!(
32            "Input `{}` queried before `db.update_input(..)` called",
33            std::any::type_name::<T>()
34        )
35    }
36
37    fn input_to_cell(_: &Self, _: &Self::Storage) -> Option<Cell> {
38        panic!(
39            "Input used without a storage type wrapper - try wrapping your type with `HashMapStorage<Input<T>>`"
40        )
41    }
42
43    fn get_function_and_output(_: Cell, _: &Self::Storage) -> (&Self, Option<&Self::Output>) {
44        panic!(
45            "Input used without a storage type wrapper - try wrapping your type with `HashMapStorage<Input<T>>`"
46        )
47    }
48
49    fn set_output(_: Cell, _: Self::Output, _: &mut Self::Storage) {
50        panic!(
51            "Input used without a storage type wrapper - try wrapping your type with `HashMapStorage<Input<T>>`"
52        )
53    }
54
55    fn insert_new_cell(_: Cell, _: Self, _: &mut Self::Storage) {
56        panic!(
57            "Input used without a storage type wrapper - try wrapping your type with `HashMapStorage<Input<T>>`"
58        )
59    }
60}