|
| 1 | +//! Accumulate values from within a tracked function. |
| 2 | +//! Then mutate the values so that the tracked function re-executes. |
| 3 | +//! Check that we accumulate the appropriate, new values. |
| 4 | +
|
| 5 | +use expect_test::expect; |
| 6 | +use salsa::{Accumulator, Setter}; |
| 7 | +use test_log::test; |
| 8 | + |
| 9 | +#[salsa::input(debug)] |
| 10 | +struct List { |
| 11 | + value: u32, |
| 12 | + next: Option<List>, |
| 13 | +} |
| 14 | + |
| 15 | +// Silence a warning about this not being used (it is). |
| 16 | +#[allow(unused)] |
| 17 | +#[salsa::accumulator] |
| 18 | +#[derive(Copy, Clone, Debug)] |
| 19 | +struct Integers(u32); |
| 20 | + |
| 21 | +#[salsa::tracked] |
| 22 | +fn compute(db: &dyn salsa::Database, input: List) { |
| 23 | + compute_single(db, input); |
| 24 | + if let Some(next) = input.next(db) { |
| 25 | + compute(db, next); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// In https://github.com/salsa-rs/salsa/issues/923 there was an issue specifically with tracked fn calling tracked fn. |
| 30 | +#[salsa::tracked] |
| 31 | +fn compute_single(db: &dyn salsa::Database, input: List) { |
| 32 | + Integers(input.value(db)).accumulate(db); |
| 33 | +} |
| 34 | + |
| 35 | +#[test] |
| 36 | +fn test1() { |
| 37 | + let mut db = salsa::DatabaseImpl::new(); |
| 38 | + |
| 39 | + let l0 = List::new(&db, 1, None); |
| 40 | + let l1 = List::new(&db, 10, Some(l0)); |
| 41 | + |
| 42 | + compute(&db, l1); |
| 43 | + expect![[r#" |
| 44 | + [ |
| 45 | + Integers( |
| 46 | + 10, |
| 47 | + ), |
| 48 | + Integers( |
| 49 | + 1, |
| 50 | + ), |
| 51 | + ] |
| 52 | + "#]] |
| 53 | + .assert_debug_eq(&compute::accumulated::<Integers>(&db, l1)); |
| 54 | + |
| 55 | + l0.set_value(&mut db).to(2); |
| 56 | + compute(&db, l1); |
| 57 | + expect![[r#" |
| 58 | + [ |
| 59 | + Integers( |
| 60 | + 10, |
| 61 | + ), |
| 62 | + Integers( |
| 63 | + 2, |
| 64 | + ), |
| 65 | + ] |
| 66 | + "#]] |
| 67 | + .assert_debug_eq(&compute::accumulated::<Integers>(&db, l1)); |
| 68 | + |
| 69 | + l1.set_value(&mut db).to(11); |
| 70 | + compute(&db, l1); |
| 71 | + expect![[r#" |
| 72 | + [ |
| 73 | + Integers( |
| 74 | + 11, |
| 75 | + ), |
| 76 | + Integers( |
| 77 | + 2, |
| 78 | + ), |
| 79 | + ] |
| 80 | + "#]] |
| 81 | + .assert_debug_eq(&compute::accumulated::<Integers>(&db, l1)); |
| 82 | +} |
0 commit comments