2022-08-16 15:04:39 +00:00
|
|
|
//! Accumulate values from within a tracked function.
|
|
|
|
//! Then mutate the values so that the tracked function re-executes.
|
|
|
|
//! Check that we accumulate the appropriate, new values.
|
|
|
|
|
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 10:04:01 +00:00
|
|
|
use salsa::{Accumulator, Setter};
|
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]
|
2024-07-19 11:08:11 +00:00
|
|
|
#[derive(Copy)]
|
2022-08-16 15:04:39 +00:00
|
|
|
struct Integers(u32);
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
|
|
|
fn compute(db: &dyn Db, input: List) {
|
|
|
|
eprintln!(
|
|
|
|
"{:?}(value={:?}, next={:?})",
|
|
|
|
input,
|
|
|
|
input.value(db),
|
|
|
|
input.next(db)
|
|
|
|
);
|
|
|
|
let result = if let Some(next) = input.next(db) {
|
|
|
|
let next_integers = compute::accumulated::<Integers>(db, next);
|
|
|
|
eprintln!("{:?}", next_integers);
|
2024-07-16 10:04:01 +00:00
|
|
|
let v = input.value(db) + next_integers.iter().map(|a| a.0).sum::<u32>();
|
2022-08-16 15:04:39 +00:00
|
|
|
eprintln!("input={:?} v={:?}", input.value(db), v);
|
|
|
|
v
|
|
|
|
} else {
|
|
|
|
input.value(db)
|
|
|
|
};
|
2024-07-16 10:04:01 +00:00
|
|
|
Integers(result).accumulate(db);
|
2022-08-16 15:04:39 +00:00
|
|
|
eprintln!("pushed result {:?}", 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 l0 = List::new(&db, 1, None);
|
|
|
|
let l1 = List::new(&db, 10, Some(l0));
|
2022-08-16 15:04:39 +00:00
|
|
|
|
|
|
|
compute(&db, l1);
|
|
|
|
expect![[r#"
|
|
|
|
[
|
2024-07-19 10:24:51 +00:00
|
|
|
Integers(
|
|
|
|
11,
|
|
|
|
),
|
|
|
|
Integers(
|
|
|
|
1,
|
|
|
|
),
|
2022-08-16 15:04:39 +00:00
|
|
|
]
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&compute::accumulated::<Integers>(&db, l1));
|
|
|
|
|
2022-08-22 10:32:04 +00:00
|
|
|
l0.set_value(&mut db).to(2);
|
2022-08-16 15:04:39 +00:00
|
|
|
compute(&db, l1);
|
|
|
|
expect![[r#"
|
|
|
|
[
|
2024-07-19 10:24:51 +00:00
|
|
|
Integers(
|
|
|
|
12,
|
|
|
|
),
|
|
|
|
Integers(
|
|
|
|
2,
|
|
|
|
),
|
2022-08-16 15:04:39 +00:00
|
|
|
]
|
|
|
|
"#]]
|
|
|
|
.assert_debug_eq(&compute::accumulated::<Integers>(&db, l1));
|
|
|
|
}
|