salsa/tests/tracked_fn_read_own_specify.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2022-09-24 00:41:55 +00:00
use expect_test::expect;
2024-06-18 07:40:21 +00:00
mod common;
use common::{LogDatabase, Logger};
use salsa::Database;
2022-09-17 12:26:20 +00:00
2024-07-16 10:04:01 +00:00
#[salsa::input]
2022-09-17 12:26:20 +00:00
struct MyInput {
field: u32,
}
2024-07-16 10:04:01 +00:00
#[salsa::tracked]
2024-05-24 01:16:30 +00:00
struct MyTracked<'db> {
2022-09-17 12:26:20 +00:00
field: u32,
}
2024-07-16 10:04:01 +00:00
#[salsa::tracked]
fn tracked_fn(db: &dyn LogDatabase, input: MyInput) -> u32 {
2024-07-17 10:41:56 +00:00
db.push_log(format!("tracked_fn({input:?})"));
2022-09-17 12:26:20 +00:00
let t = MyTracked::new(db, input.field(db) * 2);
tracked_fn_extra::specify(db, t, 2222);
tracked_fn_extra(db, t)
}
2024-07-17 10:41:56 +00:00
#[salsa::tracked(specify)]
fn tracked_fn_extra<'db>(db: &dyn LogDatabase, input: MyTracked<'db>) -> u32 {
2024-07-17 10:41:56 +00:00
db.push_log(format!("tracked_fn_extra({input:?})"));
2022-09-17 12:26:20 +00:00
0
}
#[test]
fn execute() {
let mut db: salsa::DatabaseImpl<Logger> = salsa::DatabaseImpl::default();
2022-09-24 00:41:55 +00:00
let input = MyInput::new(&db, 22);
2022-09-17 12:26:20 +00:00
assert_eq!(tracked_fn(&db, input), 2222);
2022-09-24 00:41:55 +00:00
db.assert_logs(expect![[r#"
[
"tracked_fn(MyInput { [salsa id]: Id(0), field: 22 })",
2022-09-24 00:41:55 +00:00
]"#]]);
// A "synthetic write" causes the system to act *as though* some
// input of durability `durability` has changed.
db.synthetic_write(salsa::Durability::LOW);
2022-09-17 12:26:20 +00:00
2022-09-24 00:41:55 +00:00
// Re-run the query on the original input. Nothing re-executes!
2022-09-17 12:26:20 +00:00
assert_eq!(tracked_fn(&db, input), 2222);
2022-09-24 00:41:55 +00:00
db.assert_logs(expect!["[]"]);
2022-09-17 12:26:20 +00:00
}