File: debug.rs

package info (click to toggle)
rust-salsa 0.23.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,424 kB
  • sloc: sh: 12; makefile: 2; javascript: 1
file content (98 lines) | stat: -rw-r--r-- 2,575 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! Test that `DeriveWithDb` is correctly derived.

use expect_test::expect;
use salsa::{Database, Setter};

#[salsa::input(debug)]
struct MyInput {
    field: u32,
}

#[derive(Debug, Eq, PartialEq, Clone)]
struct NotSalsa {
    field: String,
}

#[salsa::input(debug)]
struct ComplexStruct {
    my_input: MyInput,
    not_salsa: NotSalsa,
}

#[test]
fn input() {
    salsa::DatabaseImpl::new().attach(|db| {
        let input = MyInput::new(db, 22);
        let not_salsa = NotSalsa {
            field: "it's salsa time".to_string(),
        };
        let complex_struct = ComplexStruct::new(db, input, not_salsa);

        // debug includes all fields
        let actual = format!("{complex_struct:?}");
        let expected = expect![[r#"ComplexStruct { [salsa id]: Id(400), my_input: MyInput { [salsa id]: Id(0), field: 22 }, not_salsa: NotSalsa { field: "it's salsa time" } }"#]];
        expected.assert_eq(&actual);
    })
}

#[salsa::tracked]
fn leak_debug_string(_db: &dyn salsa::Database, input: MyInput) -> String {
    format!("{input:?}")
}

/// Test that field reads that occur as part of `Debug` are not tracked.
/// Intentionally leaks the debug string.
/// Don't try this at home, kids.
#[test]
fn untracked_dependencies() {
    let mut db = salsa::DatabaseImpl::new();

    let input = MyInput::new(&db, 22);

    let s = leak_debug_string(&db, input);
    expect![[r#"
        "MyInput { [salsa id]: Id(0), field: 22 }"
    "#]]
    .assert_debug_eq(&s);

    input.set_field(&mut db).to(23);

    // check that we reuse the cached result for debug string
    // even though the dependency changed.
    let s = leak_debug_string(&db, input);
    assert!(s.contains(", field: 22 }"));
}

#[salsa::tracked]
struct DerivedCustom<'db> {
    my_input: MyInput,
    value: u32,
}

impl std::fmt::Debug for DerivedCustom<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        salsa::with_attached_database(|db| {
            write!(f, "{:?} / {:?}", self.my_input(db), self.value(db))
        })
        .unwrap_or_else(|| f.debug_tuple("DerivedCustom").finish())
    }
}

#[salsa::tracked]
fn leak_derived_custom(db: &dyn salsa::Database, input: MyInput, value: u32) -> String {
    let c = DerivedCustom::new(db, input, value);
    format!("{c:?}")
}

#[test]
fn custom_debug_impl() {
    let db = salsa::DatabaseImpl::new();

    let input = MyInput::new(&db, 22);

    let s = leak_derived_custom(&db, input, 23);
    expect![[r#"
        "MyInput { [salsa id]: Id(0), field: 22 } / 23"
    "#]]
    .assert_debug_eq(&s);
}