mirror of
https://github.com/salsa-rs/salsa.git
synced 2024-11-25 04:27:52 +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.
49 lines
1.1 KiB
Rust
49 lines
1.1 KiB
Rust
//! Basic Singleton struct test:
|
|
//!
|
|
//! Singleton structs are created only once. Subsequent `get`s and `new`s after creation return the same `Id`.
|
|
|
|
use expect_test::expect;
|
|
|
|
use salsa::Database as _;
|
|
use test_log::test;
|
|
|
|
#[salsa::input(singleton)]
|
|
struct MyInput {
|
|
field: u32,
|
|
id_field: u16,
|
|
}
|
|
|
|
#[test]
|
|
fn basic() {
|
|
let db = salsa::DatabaseImpl::new();
|
|
let input1 = MyInput::new(&db, 3, 4);
|
|
let input2 = MyInput::get(&db);
|
|
|
|
assert_eq!(input1, input2);
|
|
|
|
let input3 = MyInput::try_get(&db);
|
|
assert_eq!(Some(input1), input3);
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn twice() {
|
|
let db = salsa::DatabaseImpl::new();
|
|
let input1 = MyInput::new(&db, 3, 4);
|
|
let input2 = MyInput::get(&db);
|
|
|
|
assert_eq!(input1, input2);
|
|
|
|
// should panic here
|
|
_ = MyInput::new(&db, 3, 5);
|
|
}
|
|
|
|
#[test]
|
|
fn debug() {
|
|
salsa::DatabaseImpl::new().attach(|db| {
|
|
let input = MyInput::new(db, 3, 4);
|
|
let actual = format!("{:?}", input);
|
|
let expected = expect!["MyInput { [salsa id]: Id(0), field: 3, id_field: 4 }"];
|
|
expected.assert_eq(&actual);
|
|
});
|
|
}
|