salsa/tests/tracked_with_struct_db.rs

69 lines
1.6 KiB
Rust
Raw Normal View History

//! Test that a setting a field on a `#[salsa::input]`
//! overwrites and returns the old value.
2024-07-17 12:45:49 +00:00
use salsa::Database as _;
use test_log::test;
2024-07-17 12:42:06 +00:00
#[salsa::db]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
2024-07-17 12:42:06 +00:00
#[salsa::db]
impl salsa::Database for Database {}
#[salsa::input]
struct MyInput {
field: String,
}
#[salsa::tracked]
struct MyTracked<'db> {
data: MyInput,
next: MyList<'db>,
}
2024-07-17 12:42:06 +00:00
#[derive(PartialEq, Eq, Clone, Debug, salsa::Update)]
enum MyList<'db> {
None,
Next(MyTracked<'db>),
}
#[salsa::tracked]
2024-07-17 12:42:06 +00:00
fn create_tracked_list<'db>(db: &'db dyn salsa::Database, input: MyInput) -> MyTracked<'db> {
let t0 = MyTracked::new(db, input, MyList::None);
let t1 = MyTracked::new(db, input, MyList::Next(t0));
t1
}
#[test]
fn execute() {
2024-07-17 12:45:49 +00:00
Database::default().attach(|db| {
let input = MyInput::new(db, "foo".to_string());
let t0: MyTracked = create_tracked_list(db, input);
let t1 = create_tracked_list(db, input);
expect_test::expect![[r#"
MyTracked {
[salsa id]: 1,
data: MyInput {
[salsa id]: 0,
2024-07-17 12:45:49 +00:00
field: "foo",
},
next: Next(
MyTracked {
2024-05-21 09:57:04 +00:00
[salsa id]: 0,
2024-07-17 12:45:49 +00:00
data: MyInput {
[salsa id]: 0,
field: "foo",
},
next: None,
2024-05-21 09:57:04 +00:00
},
2024-07-17 12:45:49 +00:00
),
}
"#]]
.assert_debug_eq(&t0);
assert_eq!(t0, t1);
})
}