2022-08-05 06:46:16 +00:00
|
|
|
//! Test that a `tracked` fn on a `salsa::input`
|
|
|
|
//! compiles and executes successfully.
|
2022-08-05 18:32:12 +00:00
|
|
|
#![allow(warnings)]
|
2022-08-05 06:46:16 +00:00
|
|
|
|
|
|
|
#[salsa::jar(db = Db)]
|
2024-05-24 01:16:30 +00:00
|
|
|
struct Jar(MyInput, MyTracked<'_>, tracked_fn, tracked_fn_extra);
|
2022-08-05 06:46:16 +00:00
|
|
|
|
|
|
|
trait Db: salsa::DbWithJar<Jar> {}
|
|
|
|
|
|
|
|
#[salsa::input(jar = Jar)]
|
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(jar = Jar)]
|
2024-05-24 01:16:30 +00:00
|
|
|
struct MyTracked<'db> {
|
2022-08-05 06:46:16 +00:00
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(jar = Jar)]
|
2024-05-24 01:16:30 +00:00
|
|
|
fn tracked_fn<'db>(db: &'db dyn Db, input: MyInput) -> MyTracked<'db> {
|
2022-08-05 06:46:16 +00:00
|
|
|
let t = MyTracked::new(db, input.field(db) * 2);
|
|
|
|
if input.field(db) != 0 {
|
|
|
|
tracked_fn_extra::specify(db, t, 2222);
|
|
|
|
}
|
|
|
|
t
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::tracked(jar = Jar, specify)]
|
2024-05-24 01:16:30 +00:00
|
|
|
fn tracked_fn_extra<'db>(_db: &'db dyn Db, _input: MyTracked<'db>) -> u32 {
|
2022-08-05 06:46:16 +00:00
|
|
|
0
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::db(Jar)]
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Database {
|
|
|
|
storage: salsa::Storage<Self>,
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:11:48 +00:00
|
|
|
impl salsa::Database for Database {}
|
2022-08-05 06:46:16 +00:00
|
|
|
|
|
|
|
impl Db for Database {}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_when_specified() {
|
|
|
|
let mut db = Database::default();
|
2022-09-03 21:48:24 +00:00
|
|
|
let input = MyInput::new(&db, 22);
|
2022-08-05 06:46:16 +00:00
|
|
|
let tracked = tracked_fn(&db, input);
|
|
|
|
assert_eq!(tracked.field(&db), 44);
|
|
|
|
assert_eq!(tracked_fn_extra(&db, tracked), 2222);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_when_not_specified() {
|
|
|
|
let mut db = Database::default();
|
2022-09-03 21:48:24 +00:00
|
|
|
let input = MyInput::new(&db, 0);
|
2022-08-05 06:46:16 +00:00
|
|
|
let tracked = tracked_fn(&db, input);
|
|
|
|
assert_eq!(tracked.field(&db), 0);
|
|
|
|
assert_eq!(tracked_fn_extra(&db, tracked), 0);
|
|
|
|
}
|