2022-08-16 15:04:39 +00:00
|
|
|
//! Accumulator re-use test.
|
|
|
|
//!
|
|
|
|
//! Tests behavior when a query's only inputs
|
|
|
|
//! are the accumulated values from another query.
|
|
|
|
|
2024-06-18 07:40:21 +00:00
|
|
|
mod common;
|
|
|
|
use common::{HasLogger, Logger};
|
2022-08-16 15:04:39 +00:00
|
|
|
|
|
|
|
use expect_test::expect;
|
2024-07-16 00:29:36 +00:00
|
|
|
use salsa::prelude::*;
|
2022-08-16 15:04:39 +00:00
|
|
|
use test_log::test;
|
|
|
|
|
2024-07-16 00:29:36 +00:00
|
|
|
#[salsa::db]
|
|
|
|
trait Db: salsa::Database + HasLogger {}
|
2022-08-16 15:04:39 +00:00
|
|
|
|
|
|
|
#[salsa::input]
|
|
|
|
struct List {
|
|
|
|
value: u32,
|
|
|
|
next: Option<List>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::accumulator]
|
|
|
|
struct Integers(u32);
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
|
|
|
fn compute(db: &dyn Db, input: List) -> u32 {
|
|
|
|
db.push_log(format!("compute({:?})", input,));
|
|
|
|
|
|
|
|
// always pushes 0
|
2024-07-16 00:29:36 +00:00
|
|
|
Integers(0).accumulate(db);
|
2022-08-16 15:04:39 +00:00
|
|
|
|
|
|
|
let result = if let Some(next) = input.next(db) {
|
|
|
|
let next_integers = compute::accumulated::<Integers>(db, next);
|
2024-07-16 00:29:36 +00:00
|
|
|
let v = input.value(db) + next_integers.iter().map(|i| i.0).sum::<u32>();
|
2022-08-16 15:04:39 +00:00
|
|
|
v
|
|
|
|
} else {
|
|
|
|
input.value(db)
|
|
|
|
};
|
|
|
|
|
|
|
|
// return value changes
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2024-07-16 00:29:36 +00:00
|
|
|
#[salsa::db]
|
2022-08-16 15:04:39 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct Database {
|
|
|
|
storage: salsa::Storage<Self>,
|
|
|
|
logger: Logger,
|
|
|
|
}
|
|
|
|
|
2024-07-16 00:29:36 +00:00
|
|
|
#[salsa::db]
|
2022-08-16 15:04:39 +00:00
|
|
|
impl salsa::Database for Database {
|
|
|
|
fn salsa_event(&self, _event: salsa::Event) {}
|
|
|
|
}
|
|
|
|
|
2024-07-16 00:29:36 +00:00
|
|
|
#[salsa::db]
|
2022-08-16 15:04:39 +00:00
|
|
|
impl Db for Database {}
|
|
|
|
|
|
|
|
impl HasLogger for Database {
|
|
|
|
fn logger(&self) -> &Logger {
|
|
|
|
&self.logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test1() {
|
|
|
|
let mut db = Database::default();
|
|
|
|
|
2022-09-03 21:48:24 +00:00
|
|
|
let l1 = List::new(&db, 1, None);
|
|
|
|
let l2 = List::new(&db, 2, Some(l1));
|
2022-08-16 15:04:39 +00:00
|
|
|
|
|
|
|
assert_eq!(compute(&db, l2), 2);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-07-19 10:24:51 +00:00
|
|
|
"compute(List { [salsa id]: 1, value: 2, next: Some(List { [salsa id]: 0, value: 1, next: None }) })",
|
|
|
|
"compute(List { [salsa id]: 0, value: 1, next: None })",
|
2022-08-16 15:04:39 +00:00
|
|
|
]"#]]);
|
|
|
|
|
|
|
|
// When we mutate `l1`, we should re-execute `compute` for `l1`,
|
|
|
|
// but we should not have to re-execute `compute` for `l2`.
|
2024-07-16 10:04:01 +00:00
|
|
|
// The only input for `compute(l1)` is the accumulated values from `l1`,
|
2022-08-16 15:04:39 +00:00
|
|
|
// which have not changed.
|
2022-08-22 10:32:04 +00:00
|
|
|
l1.set_value(&mut db).to(2);
|
2022-08-16 15:04:39 +00:00
|
|
|
assert_eq!(compute(&db, l2), 2);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-07-19 10:24:51 +00:00
|
|
|
"compute(List { [salsa id]: 1, value: 2, next: Some(List { [salsa id]: 0, value: 2, next: None }) })",
|
|
|
|
"compute(List { [salsa id]: 0, value: 2, next: None })",
|
2022-08-16 15:04:39 +00:00
|
|
|
]"#]]);
|
|
|
|
}
|