salsa/salsa-2022-tests/tests/tracked_fn_on_tracked_specify.rs

69 lines
1.5 KiB
Rust
Raw Normal View History

//! Test that a `tracked` fn on a `salsa::input`
//! compiles and executes successfully.
2022-08-05 18:32:12 +00:00
#![allow(warnings)]
#[salsa::jar(db = Db)]
struct Jar(MyInput, MyTracked, tracked_fn, tracked_fn_extra);
trait Db: salsa::DbWithJar<Jar> {}
#[salsa::input(jar = Jar)]
struct MyInput {
field: u32,
}
#[salsa::tracked(jar = Jar)]
struct MyTracked {
field: u32,
}
#[salsa::tracked(jar = Jar)]
fn tracked_fn(db: &dyn Db, input: MyInput) -> MyTracked {
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)]
fn tracked_fn_extra(_db: &dyn Db, _input: MyTracked) -> u32 {
0
}
#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
impl salsa::Database for Database {
fn salsa_runtime(&self) -> &salsa::Runtime {
self.storage.runtime()
}
fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime {
self.storage.runtime_mut()
}
}
impl Db for Database {}
#[test]
fn execute_when_specified() {
let mut db = Database::default();
let input = MyInput::new(&mut db, 22);
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();
let input = MyInput::new(&mut db, 0);
let tracked = tracked_fn(&db, input);
assert_eq!(tracked.field(&db), 0);
assert_eq!(tracked_fn_extra(&db, tracked), 0);
}