2022-09-02 12:09:28 +00:00
|
|
|
//! Basic Singleton struct test:
|
2022-09-02 01:54:21 +00:00
|
|
|
//!
|
2022-09-02 12:09:28 +00:00
|
|
|
//! Singleton structs are created only once. Subsequent `get`s and `new`s after creation return the same `Id`.
|
2022-09-02 01:54:21 +00:00
|
|
|
|
|
|
|
use salsa_2022_tests::{HasLogger, Logger};
|
|
|
|
|
|
|
|
use test_log::test;
|
|
|
|
|
|
|
|
#[salsa::jar(db = Db)]
|
2022-09-02 02:02:51 +00:00
|
|
|
struct Jar(MyInput);
|
2022-09-02 01:54:21 +00:00
|
|
|
|
|
|
|
trait Db: salsa::DbWithJar<Jar> + HasLogger {}
|
|
|
|
|
|
|
|
#[salsa::input(singleton)]
|
|
|
|
struct MyInput {
|
|
|
|
field: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[salsa::db(Jar)]
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Database {
|
|
|
|
storage: salsa::Storage<Self>,
|
|
|
|
logger: Logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl salsa::Database for Database {}
|
|
|
|
|
|
|
|
impl Db for Database {}
|
|
|
|
|
|
|
|
impl HasLogger for Database {
|
|
|
|
fn logger(&self) -> &Logger {
|
|
|
|
&self.logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
let mut db = Database::default();
|
|
|
|
let input1 = MyInput::new(&mut db, 3);
|
|
|
|
let input2 = MyInput::get(&db);
|
|
|
|
|
|
|
|
assert_eq!(input1, input2);
|
|
|
|
|
|
|
|
let input3 = MyInput::try_get(&db);
|
|
|
|
assert_eq!(Some(input1), input3);
|
|
|
|
|
|
|
|
let input4 = MyInput::new(&mut db, 3);
|
|
|
|
|
|
|
|
assert_eq!(input2, input4)
|
|
|
|
}
|