mirror of
https://github.com/salsa-rs/salsa.git
synced 2024-11-24 12:16:25 +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.
73 lines
1.7 KiB
Rust
73 lines
1.7 KiB
Rust
//! Accumulate values from within a tracked function.
|
|
//! Then mutate the values so that the tracked function re-executes.
|
|
//! Check that we accumulate the appropriate, new values.
|
|
|
|
use expect_test::expect;
|
|
use salsa::{Accumulator, 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 salsa::Database, input: List) {
|
|
eprintln!(
|
|
"{:?}(value={:?}, next={:?})",
|
|
input,
|
|
input.value(db),
|
|
input.next(db)
|
|
);
|
|
let result = if let Some(next) = input.next(db) {
|
|
let next_integers = compute::accumulated::<Integers>(db, next);
|
|
eprintln!("{:?}", next_integers);
|
|
let v = input.value(db) + next_integers.iter().map(|a| a.0).sum::<u32>();
|
|
eprintln!("input={:?} v={:?}", input.value(db), v);
|
|
v
|
|
} else {
|
|
input.value(db)
|
|
};
|
|
Integers(result).accumulate(db);
|
|
eprintln!("pushed result {:?}", result);
|
|
}
|
|
|
|
#[test]
|
|
fn test1() {
|
|
let mut db = salsa::DatabaseImpl::new();
|
|
|
|
let l0 = List::new(&db, 1, None);
|
|
let l1 = List::new(&db, 10, Some(l0));
|
|
|
|
compute(&db, l1);
|
|
expect![[r#"
|
|
[
|
|
Integers(
|
|
11,
|
|
),
|
|
Integers(
|
|
1,
|
|
),
|
|
]
|
|
"#]]
|
|
.assert_debug_eq(&compute::accumulated::<Integers>(&db, l1));
|
|
|
|
l0.set_value(&mut db).to(2);
|
|
compute(&db, l1);
|
|
expect![[r#"
|
|
[
|
|
Integers(
|
|
12,
|
|
),
|
|
Integers(
|
|
2,
|
|
),
|
|
]
|
|
"#]]
|
|
.assert_debug_eq(&compute::accumulated::<Integers>(&db, l1));
|
|
}
|