2022-09-24 00:41:55 +00:00
|
|
|
use expect_test::expect;
|
|
|
|
use salsa::{Database as SalsaDatabase, DebugWithDb};
|
2024-06-18 07:40:21 +00:00
|
|
|
mod common;
|
|
|
|
use common::{HasLogger, Logger};
|
2022-09-17 12:26:20 +00:00
|
|
|
|
|
|
|
#[salsa::jar(db = Db)]
|
2024-05-24 01:16:30 +00:00
|
|
|
struct Jar(MyInput, MyTracked<'_>, tracked_fn, tracked_fn_extra);
|
2022-09-17 12:26:20 +00:00
|
|
|
|
2022-09-24 00:41:55 +00:00
|
|
|
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
|
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]
|
2024-05-24 01:16:30 +00:00
|
|
|
fn tracked_fn<'db>(db: &'db dyn Db, input: MyInput) -> u32 {
|
2022-09-24 00:41:55 +00:00
|
|
|
db.push_log(format!("tracked_fn({:?})", input.debug(db)));
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(jar = Jar, specify)]
|
2024-05-24 01:16:30 +00:00
|
|
|
fn tracked_fn_extra<'db>(db: &dyn Db, input: MyTracked<'db>) -> u32 {
|
2022-09-24 00:41:55 +00:00
|
|
|
db.push_log(format!("tracked_fn_extra({:?})", input.debug(db)));
|
2022-09-17 12:26:20 +00:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::db(Jar)]
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Database {
|
|
|
|
storage: salsa::Storage<Self>,
|
2022-09-24 00:41:55 +00:00
|
|
|
logger: Logger,
|
2022-09-17 12:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl salsa::Database for Database {}
|
|
|
|
|
|
|
|
impl Db for Database {}
|
|
|
|
|
2022-09-24 00:41:55 +00:00
|
|
|
impl HasLogger for Database {
|
|
|
|
fn logger(&self) -> &Logger {
|
|
|
|
&self.logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-17 12:26:20 +00:00
|
|
|
#[test]
|
|
|
|
fn execute() {
|
|
|
|
let mut db = Database::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#"
|
|
|
|
[
|
2024-04-03 09:59:11 +00:00
|
|
|
"tracked_fn(MyInput { [salsa 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
|
|
|
}
|