mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-22 21:05:11 +00:00
daaa78056a
Under this design, *all* databases are a `DatabaseImpl<U>`, where the `U` implements `UserData` (you can use `()` if there is none). Code would default to `&dyn salsa::Database` but if you want to give access to the userdata, you can define a custom database trait `MyDatabase: salsa::Databse` so long as you * annotate `MyDatabase` trait definition of impls of `MyDatabase` with `#[salsa::db]` * implement `MyDatabase` for `DatabaseImpl<U>` where `U` is your userdata (this could be a blanket impl, if you don't know the precise userdata type). The `tests/common/mod.rs` shows the pattern.
49 lines
1 KiB
Rust
49 lines
1 KiB
Rust
mod common;
|
|
|
|
use common::{LogDatabase, Logger};
|
|
use expect_test::expect;
|
|
use salsa::{DatabaseImpl, Setter as _};
|
|
|
|
#[salsa::input]
|
|
struct Input {
|
|
number: i16,
|
|
}
|
|
|
|
#[salsa::tracked(no_eq)]
|
|
fn abs_float(db: &dyn LogDatabase, input: Input) -> f32 {
|
|
let number = input.number(db);
|
|
|
|
db.push_log(format!("abs_float({number})"));
|
|
number.abs() as f32
|
|
}
|
|
|
|
#[salsa::tracked]
|
|
fn derived(db: &dyn LogDatabase, input: Input) -> u32 {
|
|
let x = abs_float(db, input);
|
|
db.push_log("derived".to_string());
|
|
|
|
x as u32
|
|
}
|
|
#[test]
|
|
fn invoke() {
|
|
let mut db: DatabaseImpl<Logger> = Default::default();
|
|
|
|
let input = Input::new(&db, 5);
|
|
let x = derived(&db, input);
|
|
|
|
assert_eq!(x, 5);
|
|
|
|
input.set_number(&mut db).to(-5);
|
|
|
|
// Derived should re-execute even the result of `abs_float` is the same.
|
|
let x = derived(&db, input);
|
|
assert_eq!(x, 5);
|
|
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"abs_float(5)",
|
|
"derived",
|
|
"abs_float(-5)",
|
|
"derived",
|
|
]"#]]);
|
|
}
|