mirror of
https://github.com/salsa-rs/salsa.git
synced 2024-11-24 20:20:26 +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.
99 lines
1.9 KiB
Rust
99 lines
1.9 KiB
Rust
//! Basic deletion test:
|
|
//!
|
|
//! * entities not created in a revision are deleted, as is any memoized data keyed on them.
|
|
|
|
mod common;
|
|
|
|
use salsa::{Database, Setter};
|
|
use test_log::test;
|
|
|
|
#[salsa::input]
|
|
struct MyInput {
|
|
identity: u32,
|
|
}
|
|
|
|
#[salsa::tracked]
|
|
struct MyTracked<'db> {
|
|
#[id]
|
|
identifier: u32,
|
|
|
|
#[return_ref]
|
|
field: Bomb,
|
|
}
|
|
|
|
thread_local! {
|
|
static DROPPED: std::cell::RefCell<Vec<u32>> = const { std::cell::RefCell::new(vec![]) };
|
|
}
|
|
|
|
fn dropped() -> Vec<u32> {
|
|
DROPPED.with(|d| d.borrow().clone())
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
struct Bomb {
|
|
identity: u32,
|
|
}
|
|
|
|
impl Drop for Bomb {
|
|
fn drop(&mut self) {
|
|
DROPPED.with(|d| d.borrow_mut().push(self.identity));
|
|
}
|
|
}
|
|
|
|
#[salsa::tracked]
|
|
impl MyInput {
|
|
#[salsa::tracked]
|
|
fn create_tracked_struct(self, db: &dyn Database) -> MyTracked<'_> {
|
|
MyTracked::new(
|
|
db,
|
|
self.identity(db),
|
|
Bomb {
|
|
identity: self.identity(db),
|
|
},
|
|
)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn deletion_drops() {
|
|
let mut db = salsa::DatabaseImpl::new();
|
|
|
|
let input = MyInput::new(&db, 22);
|
|
|
|
expect_test::expect![[r#"
|
|
[]
|
|
"#]]
|
|
.assert_debug_eq(&dropped());
|
|
|
|
let tracked_struct = input.create_tracked_struct(&db);
|
|
assert_eq!(tracked_struct.field(&db).identity, 22);
|
|
|
|
expect_test::expect![[r#"
|
|
[]
|
|
"#]]
|
|
.assert_debug_eq(&dropped());
|
|
|
|
input.set_identity(&mut db).to(44);
|
|
|
|
expect_test::expect![[r#"
|
|
[]
|
|
"#]]
|
|
.assert_debug_eq(&dropped());
|
|
|
|
let tracked_struct = input.create_tracked_struct(&db);
|
|
assert_eq!(tracked_struct.field(&db).identity, 44);
|
|
|
|
expect_test::expect![[r#"
|
|
[]
|
|
"#]]
|
|
.assert_debug_eq(&dropped());
|
|
|
|
input.set_identity(&mut db).to(66);
|
|
|
|
expect_test::expect![[r#"
|
|
[
|
|
22,
|
|
]
|
|
"#]]
|
|
.assert_debug_eq(&dropped());
|
|
}
|