2022-08-06 06:26:22 +00:00
|
|
|
//! Test that a `tracked` fn on a `salsa::input`
|
|
|
|
//! compiles and executes successfully.
|
|
|
|
|
2024-06-18 07:40:21 +00:00
|
|
|
mod common;
|
|
|
|
use common::{HasLogger, Logger};
|
2022-08-06 14:42:04 +00:00
|
|
|
|
2022-08-06 06:26:22 +00:00
|
|
|
use expect_test::expect;
|
2024-07-15 11:46:23 +00:00
|
|
|
use salsa::Setter;
|
2022-08-07 07:01:39 +00:00
|
|
|
use test_log::test;
|
2022-08-06 06:26:22 +00:00
|
|
|
|
2024-07-14 17:36:30 +00:00
|
|
|
#[salsa::db]
|
2024-07-15 11:46:23 +00:00
|
|
|
trait Db: salsa::Database + HasLogger {}
|
2022-08-06 06:26:22 +00:00
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::input]
|
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
2022-08-06 06:26:22 +00:00
|
|
|
}
|
|
|
|
|
2024-07-11 10:20:53 +00:00
|
|
|
#[salsa::tracked]
|
2022-08-06 06:26:22 +00:00
|
|
|
fn final_result(db: &dyn Db, input: MyInput) -> u32 {
|
|
|
|
db.push_log(format!("final_result({:?})", input));
|
|
|
|
intermediate_result(db, input).field(db) * 2
|
|
|
|
}
|
|
|
|
|
2024-07-11 10:20:53 +00:00
|
|
|
#[salsa::tracked]
|
2024-04-27 20:25:19 +00:00
|
|
|
struct MyTracked<'db> {
|
2022-08-06 06:26:22 +00:00
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
2024-07-11 10:20:53 +00:00
|
|
|
#[salsa::tracked]
|
2024-07-19 13:13:48 +00:00
|
|
|
fn intermediate_result(db: &dyn Db, input: MyInput) -> MyTracked<'_> {
|
2022-08-06 06:26:22 +00:00
|
|
|
db.push_log(format!("intermediate_result({:?})", input));
|
|
|
|
MyTracked::new(db, input.field(db) / 2)
|
|
|
|
}
|
|
|
|
|
2024-07-11 10:20:53 +00:00
|
|
|
#[salsa::db]
|
2022-08-06 06:26:22 +00:00
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2024-07-14 17:49:59 +00:00
|
|
|
#[salsa::db]
|
2022-08-24 12:11:48 +00:00
|
|
|
impl salsa::Database for Database {}
|
2022-08-06 06:26:22 +00:00
|
|
|
|
2024-07-14 17:49:59 +00:00
|
|
|
#[salsa::db]
|
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]
|
|
|
|
fn execute() {
|
|
|
|
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-07-19 12:10:23 +00:00
|
|
|
"final_result(MyInput { [salsa id]: 0, field: 22 })",
|
|
|
|
"intermediate_result(MyInput { [salsa id]: 0, field: 22 })",
|
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-07-19 12:10:23 +00:00
|
|
|
"intermediate_result(MyInput { [salsa id]: 0, field: 23 })",
|
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-07-19 12:10:23 +00:00
|
|
|
"intermediate_result(MyInput { [salsa id]: 0, field: 24 })",
|
|
|
|
"final_result(MyInput { [salsa id]: 0, field: 24 })",
|
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-07-19 12:10:23 +00:00
|
|
|
"final_result(MyInput { [salsa id]: 0, field: 22 })",
|
|
|
|
"intermediate_result(MyInput { [salsa id]: 0, field: 22 })",
|
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#"
|
|
|
|
[]"#]]);
|
|
|
|
}
|