inc_complete/computation/
intermediate.rs

1use crate::{Cell, DbHandle};
2
3use super::Computation;
4
5/// A helper type for defining intermediate Computations.
6/// This will consist of any non-input in a program. These are
7/// always functions which are cached and derive their value from
8/// other functions or inputs.
9#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
10pub struct Intermediate<T>(T);
11
12impl<T> Intermediate<T> {
13    pub const fn new(x: T) -> Self {
14        Self(x)
15    }
16}
17
18pub trait Run {
19    type Output: Eq;
20
21    fn run(&self, handle: &mut DbHandle<impl Computation>) -> Self::Output;
22}
23
24impl<T> Computation for Intermediate<T>
25where
26    T: Run + Clone + 'static,
27{
28    type Output = <T as Run>::Output;
29    type Storage = ();
30
31    fn run(&self, handle: &mut DbHandle<impl Computation>) -> Self::Output {
32        self.0.run(handle)
33    }
34
35    fn input_to_cell(_: &Self, _: &Self::Storage) -> Option<Cell> {
36        panic!("Intermediate used without a storage type wrapper - try wrapping your type with `HashMapStorage<Intermediate<T>>`")
37    }
38
39    fn get_function_and_output(
40        _: Cell,
41        _: &Self::Storage,
42    ) -> (&Self, Option<&Self::Output>) {
43        panic!("Intermediate used without a storage type wrapper - try wrapping your type with `HashMapStorage<Intermediate<T>>`")
44    }
45
46    fn set_output(_: Cell, _: Self::Output, _: &mut Self::Storage) {
47        panic!("Intermediate used without a storage type wrapper - try wrapping your type with `HashMapStorage<Intermediate<T>>`")
48    }
49
50    fn insert_new_cell(_: Cell, _: Self, _: &mut Self::Storage) {
51        panic!("Intermediate used without a storage type wrapper - try wrapping your type with `HashMapStorage<Intermediate<T>>`")
52    }
53}