2018-10-01 10:47:24 +00:00
|
|
|
#[derive(Default)]
|
2018-10-05 08:54:51 +00:00
|
|
|
pub struct DatabaseImpl {
|
2018-10-07 11:08:22 +00:00
|
|
|
runtime: salsa::Runtime<DatabaseImpl>,
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 08:54:51 +00:00
|
|
|
impl salsa::Database for DatabaseImpl {
|
2018-10-07 11:08:22 +00:00
|
|
|
fn salsa_runtime(&self) -> &salsa::Runtime<DatabaseImpl> {
|
2018-10-01 10:47:24 +00:00
|
|
|
&self.runtime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 08:54:51 +00:00
|
|
|
salsa::database_storage! {
|
|
|
|
pub struct DatabaseImplStorage for DatabaseImpl {
|
|
|
|
impl Database {
|
2018-10-01 10:47:24 +00:00
|
|
|
fn memoized_a() for MemoizedA;
|
|
|
|
fn memoized_b() for MemoizedB;
|
|
|
|
fn volatile_a() for VolatileA;
|
|
|
|
fn volatile_b() for VolatileB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:35:50 +00:00
|
|
|
salsa::query_group! {
|
2018-10-05 08:54:51 +00:00
|
|
|
trait Database: salsa::Database {
|
2018-10-01 10:47:24 +00:00
|
|
|
// `a` and `b` depend on each other and form a cycle
|
2018-10-19 01:26:48 +00:00
|
|
|
fn memoized_a() -> () {
|
2018-10-05 09:28:51 +00:00
|
|
|
type MemoizedA;
|
|
|
|
}
|
2018-10-19 01:26:48 +00:00
|
|
|
fn memoized_b() -> () {
|
2018-10-05 09:28:51 +00:00
|
|
|
type MemoizedB;
|
|
|
|
}
|
2018-10-19 01:26:48 +00:00
|
|
|
fn volatile_a() -> () {
|
2018-10-05 09:28:51 +00:00
|
|
|
type VolatileA;
|
2018-10-05 09:51:18 +00:00
|
|
|
storage volatile;
|
2018-10-05 09:28:51 +00:00
|
|
|
}
|
2018-10-19 01:26:48 +00:00
|
|
|
fn volatile_b() -> () {
|
2018-10-05 09:28:51 +00:00
|
|
|
type VolatileB;
|
2018-10-05 09:51:18 +00:00
|
|
|
storage volatile;
|
2018-10-05 09:28:51 +00:00
|
|
|
}
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
fn memoized_a(db: &impl Database) -> () {
|
|
|
|
db.memoized_b()
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
fn memoized_b(db: &impl Database) -> () {
|
|
|
|
db.memoized_a()
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
fn volatile_a(db: &impl Database) -> () {
|
|
|
|
db.volatile_b()
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
fn volatile_b(db: &impl Database) -> () {
|
|
|
|
db.volatile_a()
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "cycle detected")]
|
|
|
|
fn cycle_memoized() {
|
2018-10-05 08:54:51 +00:00
|
|
|
let query = DatabaseImpl::default();
|
2018-10-19 01:26:48 +00:00
|
|
|
query.memoized_a();
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic(expected = "cycle detected")]
|
|
|
|
fn cycle_volatile() {
|
2018-10-05 08:54:51 +00:00
|
|
|
let query = DatabaseImpl::default();
|
2018-10-19 01:26:48 +00:00
|
|
|
query.volatile_a();
|
2018-10-01 10:47:24 +00:00
|
|
|
}
|