2024-05-30 13:40:21 +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;
|
2024-07-27 12:29:41 +00:00
|
|
|
use common::{LogDatabase, Logger};
|
2024-05-30 13:40:21 +00:00
|
|
|
|
|
|
|
use expect_test::expect;
|
2024-07-27 12:29:41 +00:00
|
|
|
use salsa::{DatabaseImpl, Setter};
|
2024-05-30 13:40:21 +00:00
|
|
|
use test_log::test;
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::input]
|
2024-05-30 13:40:21 +00:00
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::tracked]
|
2024-07-27 12:29:41 +00:00
|
|
|
fn final_result(db: &dyn LogDatabase, input: MyInput) -> u32 {
|
2024-05-30 13:40:21 +00:00
|
|
|
db.push_log(format!("final_result({:?})", input));
|
|
|
|
intermediate_result(db, input).field(db) * 2
|
|
|
|
}
|
|
|
|
|
2024-07-15 11:46:23 +00:00
|
|
|
#[salsa::tracked]
|
2024-05-30 13:40:21 +00:00
|
|
|
struct MyTracked<'db> {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked]
|
2024-07-27 12:29:41 +00:00
|
|
|
fn intermediate_result(db: &dyn LogDatabase, input: MyInput) -> MyTracked<'_> {
|
2024-05-30 13:40:21 +00:00
|
|
|
db.push_log(format!("intermediate_result({:?})", input));
|
|
|
|
MyTracked::new(db, input.field(db) / 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute() {
|
2024-07-27 12:29:41 +00:00
|
|
|
let mut db: DatabaseImpl<Logger> = Default::default();
|
2024-05-30 13:40:21 +00:00
|
|
|
|
|
|
|
let input = MyInput::new(&db, 22);
|
|
|
|
assert_eq!(final_result(&db, input), 22);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-06-20 12:45:38 +00:00
|
|
|
"final_result(MyInput { [salsa id]: Id(0), field: 22 })",
|
|
|
|
"intermediate_result(MyInput { [salsa id]: Id(0), field: 22 })",
|
2024-05-30 13:40:21 +00:00
|
|
|
]"#]]);
|
|
|
|
|
|
|
|
// Intermediate result is the same, so final result does
|
|
|
|
// not need to be recomputed:
|
|
|
|
input.set_field(&mut db).to(23);
|
|
|
|
assert_eq!(final_result(&db, input), 22);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-06-20 12:45:38 +00:00
|
|
|
"intermediate_result(MyInput { [salsa id]: Id(0), field: 23 })",
|
2024-05-30 13:40:21 +00:00
|
|
|
]"#]]);
|
|
|
|
|
|
|
|
input.set_field(&mut db).to(24);
|
|
|
|
assert_eq!(final_result(&db, input), 24);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
|
|
[
|
2024-06-20 12:45:38 +00:00
|
|
|
"intermediate_result(MyInput { [salsa id]: Id(0), field: 24 })",
|
|
|
|
"final_result(MyInput { [salsa id]: Id(0), field: 24 })",
|
2024-05-30 13:40:21 +00:00
|
|
|
]"#]]);
|
|
|
|
}
|