|
| 1 | +//! Tests that code using the builder's durability methods compiles. |
| 2 | +
|
| 3 | +use salsa::{Database, Durability, Setter}; |
| 4 | +use test_log::test; |
| 5 | + |
| 6 | +#[salsa::input] |
| 7 | +struct N { |
| 8 | + value: u32, |
| 9 | +} |
| 10 | + |
| 11 | +#[salsa::tracked] |
| 12 | +fn add3(db: &dyn Database, a: N, b: N, c: N) -> u32 { |
| 13 | + add(db, a, b) + c.value(db) |
| 14 | +} |
| 15 | + |
| 16 | +#[salsa::tracked] |
| 17 | +fn add(db: &dyn Database, a: N, b: N) -> u32 { |
| 18 | + a.value(db) + b.value(db) |
| 19 | +} |
| 20 | + |
| 21 | +#[test] |
| 22 | +fn durable_to_less_durable() { |
| 23 | + let mut db = salsa::DatabaseImpl::new(); |
| 24 | + |
| 25 | + let a = N::builder(11).value_durability(Durability::HIGH).new(&db); |
| 26 | + let b = N::builder(22).value_durability(Durability::HIGH).new(&db); |
| 27 | + let c = N::builder(33).value_durability(Durability::HIGH).new(&db); |
| 28 | + |
| 29 | + // Here, `add3` invokes `add(a, b)`, which yields 33. |
| 30 | + assert_eq!(add3(&db, a, b, c), 66); |
| 31 | + |
| 32 | + a.set_value(&mut db).with_durability(Durability::LOW).to(11); |
| 33 | + |
| 34 | + // Here, `add3` invokes `add`, which *still* yields 33, but which |
| 35 | + // is no longer of high durability. Since value didn't change, we might |
| 36 | + // preserve `add3` unchanged, not noticing that it is no longer |
| 37 | + // of high durability. |
| 38 | + |
| 39 | + assert_eq!(add3(&db, a, b, c), 66); |
| 40 | + |
| 41 | + // In that case, we would not get the correct result here, when |
| 42 | + // 'a' changes *again*. |
| 43 | + |
| 44 | + a.set_value(&mut db).to(22); |
| 45 | + |
| 46 | + assert_eq!(add3(&db, a, b, c), 77); |
| 47 | +} |
0 commit comments