define_intermediate

Macro define_intermediate 

Source
macro_rules! define_intermediate {
    ( $id:tt, $type_name:ident -> $output_type:ty, $( $storage_type:ty )|+, $run_function:expr) => { ... };
    ( $id:tt, assume_changed $type_name:ident -> $output_type:ty, $( $storage_type:ty )|+, $run_function:expr) => { ... };
    (@ $id:tt, $type_name:ident -> $output_type:ty, $assume_changed:expr, $( $storage_type:ty )|+, $run_function:expr) => { ... };
}
Expand description

Helper macro to define an intermediate computation type. This will implement Computation, and Run.

This macro supports multiple Storage types used, separated by |, in case your program uses multiple databases with differing storage types.

Signature: define_intermediate!(computation_id, ComputationType -> OutputType, StorageType ( | MoreStorageTypes)*, run_function)

Example usage:

#[derive(Debug, Clone)]
struct Double;
#[derive(Debug, Clone)]
struct More;

// Define `Double` as a computation with id 1 and the given run function which returns an `i32`
// to be used with a `Db<MyStorageType>` or `DbHandle<MyStorageType>`.
// The type annotations on the closure are unnecessary.
// We also may provide an existing function instead of a closure.
define_intermediate!(1, Double -> i32, MyStorageType, |_: &Double, db: &DbHandle<MyStorageType>| {
    db.get(MyInput) * 2
});

// It is also possible to signal that the value always changes with the assume_changed keyword.
// Doing so let's us avoid expensive `Eq` checks on large values which are expected to change whenever their inputs do anyway:
define_intermediate!(2, assume_changed More -> i32, MyStorageType, |_, db: &DbHandle<MyStorageType>| {
    db.get(Double) + 1
});