2022-08-06 06:26:22 +00:00
|
|
|
//! Test that a `tracked` fn on a `salsa::input`
|
|
|
|
//! compiles and executes successfully.
|
2022-08-06 14:42:04 +00:00
|
|
|
|
2022-08-06 06:26:22 +00:00
|
|
|
use expect_test::expect;
|
2022-08-07 14:39:07 +00:00
|
|
|
use salsa_2022_tests::{HasLogger, Logger};
|
|
|
|
use test_log::test;
|
2022-08-06 06:26:22 +00:00
|
|
|
|
|
|
|
#[salsa::jar(db = Db)]
|
|
|
|
struct Jar(MyInput, MyTracked, final_result, intermediate_result);
|
|
|
|
|
2022-08-06 14:42:04 +00:00
|
|
|
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
|
2022-08-06 06:26:22 +00:00
|
|
|
|
|
|
|
#[salsa::input(jar = Jar)]
|
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(jar = Jar)]
|
|
|
|
fn final_result(db: &dyn Db, input: MyInput) -> u32 {
|
|
|
|
db.push_log(format!("final_result({:?})", input));
|
|
|
|
intermediate_result(db, input).field(db) * 2
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(jar = Jar)]
|
|
|
|
struct MyTracked {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(jar = Jar)]
|
|
|
|
fn intermediate_result(db: &dyn Db, input: MyInput) -> MyTracked {
|
|
|
|
db.push_log(format!("intermediate_result({:?})", input));
|
2022-08-06 14:42:04 +00:00
|
|
|
let tracked = MyTracked::new(db, input.field(db) / 2);
|
|
|
|
let _ = tracked.field(db); // read the field of an entity we created
|
|
|
|
tracked
|
2022-08-06 06:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::db(Jar)]
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Database {
|
|
|
|
storage: salsa::Storage<Self>,
|
2022-08-06 14:42:04 +00:00
|
|
|
logger: Logger,
|
2022-08-06 06:26:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-24 12:11:48 +00:00
|
|
|
impl salsa::Database for Database {}
|
2022-08-06 06:26:22 +00:00
|
|
|
|
2022-08-06 14:42:04 +00:00
|
|
|
impl Db for Database {}
|
|
|
|
|
|
|
|
impl HasLogger for Database {
|
|
|
|
fn logger(&self) -> &Logger {
|
|
|
|
&self.logger
|
2022-08-06 06:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-08-07 04:32:56 +00:00
|
|
|
fn one_entity() {
|
2022-08-06 06:26:22 +00:00
|
|
|
let mut db = Database::default();
|
|
|
|
|
2022-09-03 21:48:24 +00:00
|
|
|
let input = MyInput::new(&db, 22);
|
2022-08-06 06:26:22 +00:00
|
|
|
assert_eq!(final_result(&db, input), 22);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-05-21 09:47:13 +00:00
|
|
|
"final_result(MyInput { [salsa id]: 0 })",
|
|
|
|
"intermediate_result(MyInput { [salsa id]: 0 })",
|
2022-08-06 06:26:22 +00:00
|
|
|
]"#]]);
|
|
|
|
|
|
|
|
// Intermediate result is the same, so final result does
|
|
|
|
// not need to be recomputed:
|
2022-08-22 10:32:04 +00:00
|
|
|
input.set_field(&mut db).to(23);
|
2022-08-06 06:26:22 +00:00
|
|
|
assert_eq!(final_result(&db, input), 22);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-05-21 09:47:13 +00:00
|
|
|
"intermediate_result(MyInput { [salsa id]: 0 })",
|
2022-08-06 06:26:22 +00:00
|
|
|
]"#]]);
|
|
|
|
|
2022-08-22 10:32:04 +00:00
|
|
|
input.set_field(&mut db).to(24);
|
2022-08-06 06:26:22 +00:00
|
|
|
assert_eq!(final_result(&db, input), 24);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-05-21 09:47:13 +00:00
|
|
|
"intermediate_result(MyInput { [salsa id]: 0 })",
|
|
|
|
"final_result(MyInput { [salsa id]: 0 })",
|
2022-08-06 06:26:22 +00:00
|
|
|
]"#]]);
|
|
|
|
}
|
2022-08-07 04:32:56 +00:00
|
|
|
|
2022-08-10 04:09:34 +00:00
|
|
|
/// Create and mutate a distinct input. No re-execution required.
|
|
|
|
#[test]
|
|
|
|
fn red_herring() {
|
|
|
|
let mut db = Database::default();
|
|
|
|
|
2022-09-03 21:48:24 +00:00
|
|
|
let input = MyInput::new(&db, 22);
|
2022-08-10 04:09:34 +00:00
|
|
|
assert_eq!(final_result(&db, input), 22);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-05-21 09:47:13 +00:00
|
|
|
"final_result(MyInput { [salsa id]: 0 })",
|
|
|
|
"intermediate_result(MyInput { [salsa id]: 0 })",
|
2022-08-10 04:09:34 +00:00
|
|
|
]"#]]);
|
|
|
|
|
|
|
|
// Create a distinct input and mutate it.
|
|
|
|
// This will trigger a new revision in the database
|
|
|
|
// but shouldn't actually invalidate our existing ones.
|
2022-09-03 21:48:24 +00:00
|
|
|
let input2 = MyInput::new(&db, 44);
|
2022-08-22 10:32:04 +00:00
|
|
|
input2.set_field(&mut db).to(66);
|
2022-08-10 04:09:34 +00:00
|
|
|
|
|
|
|
// Re-run the query on the original input. Nothing re-executes!
|
|
|
|
assert_eq!(final_result(&db, input), 22);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[]"#]]);
|
|
|
|
}
|