salsa/tests/gc/group.rs
Fabian Schuiki 93c30a953d make query_group macro procedural
Switch to a procedural implementation of the `query_group!` macro,
residing in the `components/salsa_macros` subcrate.

Allow the user to override the invoked function via `salsa::invoke(...)`
and the name of the generated query type via `salsa::query_type(...)`.

In all tests, replace the `salsa::query_group! { ... }` invocations with
the new attribute-style `#[salsa::query_group]` macro, and change them
to the new naming scheme for query types (`...Query`).

Update README, examples, and documentation.
2019-01-17 07:24:18 +01:00

56 lines
1.2 KiB
Rust

use crate::log::HasLog;
#[salsa::query_group]
pub(crate) trait GcDatabase: salsa::Database + HasLog {
#[salsa::input]
fn min(&self) -> usize;
#[salsa::input]
fn max(&self) -> usize;
#[salsa::input]
fn use_triangular(&self, key: usize) -> bool;
fn fibonacci(&self, key: usize) -> usize;
fn triangular(&self, key: usize) -> usize;
fn compute(&self, key: usize) -> usize;
fn compute_all(&self) -> Vec<usize>;
}
fn fibonacci(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("fibonacci({:?})", key));
if key == 0 {
0
} else if key == 1 {
1
} else {
db.fibonacci(key - 1) + db.fibonacci(key - 2)
}
}
fn triangular(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("triangular({:?})", key));
if key == 0 {
0
} else {
db.triangular(key - 1) + key
}
}
fn compute(db: &impl GcDatabase, key: usize) -> usize {
db.log().add(format!("compute({:?})", key));
if db.use_triangular(key) {
db.triangular(key)
} else {
db.fibonacci(key)
}
}
fn compute_all(db: &impl GcDatabase) -> Vec<usize> {
db.log().add("compute_all()");
(db.min()..db.max()).map(|v| db.compute(v)).collect()
}