mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-23 05:07:27 +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.
75 lines
2 KiB
Rust
75 lines
2 KiB
Rust
//! Demonstrates the workaround of wrapping calls to
|
|
//! `accumulated` in a tracked function to get better
|
|
//! reuse.
|
|
|
|
mod common;
|
|
use common::{LogDatabase, Logger};
|
|
|
|
use expect_test::expect;
|
|
use salsa::{Accumulator, DatabaseImpl, Setter};
|
|
use test_log::test;
|
|
|
|
#[salsa::input]
|
|
struct List {
|
|
value: u32,
|
|
next: Option<List>,
|
|
}
|
|
|
|
#[salsa::accumulator]
|
|
#[derive(Copy)]
|
|
struct Integers(u32);
|
|
|
|
#[salsa::tracked]
|
|
fn compute(db: &dyn LogDatabase, input: List) -> u32 {
|
|
db.push_log(format!("compute({:?})", input,));
|
|
|
|
// always pushes 0
|
|
Integers(0).accumulate(db);
|
|
|
|
let result = if let Some(next) = input.next(db) {
|
|
let next_integers = accumulated(db, next);
|
|
let v = input.value(db) + next_integers.iter().sum::<u32>();
|
|
v
|
|
} else {
|
|
input.value(db)
|
|
};
|
|
|
|
// return value changes
|
|
result
|
|
}
|
|
|
|
#[salsa::tracked(return_ref)]
|
|
fn accumulated(db: &dyn LogDatabase, input: List) -> Vec<u32> {
|
|
db.push_log(format!("accumulated({:?})", input));
|
|
compute::accumulated::<Integers>(db, input)
|
|
.into_iter()
|
|
.map(|a| a.0)
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn test1() {
|
|
let mut db: DatabaseImpl<Logger> = DatabaseImpl::default();
|
|
|
|
let l1 = List::new(&db, 1, None);
|
|
let l2 = List::new(&db, 2, Some(l1));
|
|
|
|
assert_eq!(compute(&db, l2), 2);
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"compute(List { [salsa id]: Id(1), value: 2, next: Some(List { [salsa id]: Id(0), value: 1, next: None }) })",
|
|
"accumulated(List { [salsa id]: Id(0), value: 1, next: None })",
|
|
"compute(List { [salsa id]: Id(0), value: 1, next: None })",
|
|
]"#]]);
|
|
|
|
// When we mutate `l1`, we should re-execute `compute` for `l1`,
|
|
// and we re-execute accumulated for `l1`, but we do NOT re-execute
|
|
// `compute` for `l2`.
|
|
l1.set_value(&mut db).to(2);
|
|
assert_eq!(compute(&db, l2), 2);
|
|
db.assert_logs(expect![[r#"
|
|
[
|
|
"accumulated(List { [salsa id]: Id(0), value: 2, next: None })",
|
|
"compute(List { [salsa id]: Id(0), value: 2, next: None })",
|
|
]"#]]);
|
|
}
|