add runtime_mut and synthetic_write to HasJarsDyn

This commit is contained in:
XFFXFF 2022-08-24 20:01:00 +08:00
parent ea0df0fc03
commit f5b0ff59d3
3 changed files with 24 additions and 12 deletions

View file

@ -120,6 +120,10 @@ fn has_jars_dyn_impl(input: &syn::ItemStruct, storage: &syn::Ident) -> syn::Item
self.#storage.runtime()
}
fn runtime_mut(&mut self) ->&mut salsa::Runtime {
self.#storage.runtime_mut()
}
fn maybe_changed_after(
&self,
input: salsa::key::DependencyIndex,
@ -159,10 +163,15 @@ fn has_jars_dyn_impl(input: &syn::ItemStruct, storage: &syn::Ident) -> syn::Item
let ingredient = self.#storage.ingredient(ingredient);
ingredient.salsa_struct_deleted(self, id);
}
fn fmt_index(&self, index: salsa::key::DependencyIndex, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let ingredient = self.#storage.ingredient(index.ingredient_index());
ingredient.fmt_index(index.key_index(), fmt)
}
fn synthetic_write(&mut self, durability: salsa::Durability) {
self.runtime_mut().synthetic_write(durability);
}
}
}
}

View file

@ -8,7 +8,7 @@ use crate::jar::Jar;
use crate::key::DependencyIndex;
use crate::runtime::local_state::QueryOrigin;
use crate::runtime::Runtime;
use crate::{Database, DatabaseKeyIndex, Id, IngredientIndex};
use crate::{Database, DatabaseKeyIndex, Id, IngredientIndex, Durability};
use super::routes::Routes;
use super::{ParallelDatabase, Revision};
@ -203,6 +203,8 @@ pub trait HasJar<J> {
pub trait HasJarsDyn {
fn runtime(&self) -> &Runtime;
fn runtime_mut(&mut self) -> &mut Runtime;
fn maybe_changed_after(&self, input: DependencyIndex, revision: Revision) -> bool;
fn cycle_recovery_strategy(&self, input: IngredientIndex) -> CycleRecoveryStrategy;
@ -226,6 +228,8 @@ pub trait HasJarsDyn {
fn salsa_struct_deleted(&self, ingredient: IngredientIndex, id: Id);
fn fmt_index(&self, index: DependencyIndex, fmt: &mut fmt::Formatter<'_>) -> fmt::Result;
fn synthetic_write(&mut self, durability: Durability);
}
// ANCHOR_END: HasJarsDyn

View file

@ -6,9 +6,9 @@ use std::sync::{
Arc,
};
use salsa::Database;
use salsa_2022_tests::{HasLogger, Logger};
use test_log::test;
use salsa::storage::HasJarsDyn;
#[salsa::jar(db = Db)]
struct Jar(MyInput, get_hot_potato, get_hot_potato2, get_volatile);
@ -61,12 +61,12 @@ fn get_volatile(db: &dyn Db, _input: MyInput) -> usize {
#[salsa::db(Jar)]
#[derive(Default)]
struct DatabaseImpl {
struct Database {
storage: salsa::Storage<Self>,
logger: Logger,
}
impl salsa::Database for DatabaseImpl {
impl salsa::Database for Database {
fn salsa_runtime(&self) -> &salsa::Runtime {
self.storage.runtime()
}
@ -76,9 +76,9 @@ impl salsa::Database for DatabaseImpl {
}
}
impl Db for DatabaseImpl {}
impl Db for Database {}
impl HasLogger for DatabaseImpl {
impl HasLogger for Database {
fn logger(&self) -> &Logger {
&self.logger
}
@ -90,7 +90,7 @@ fn load_n_potatoes() -> usize {
#[test]
fn lru_works() {
let mut db = DatabaseImpl::default();
let mut db = Database::default();
assert_eq!(load_n_potatoes(), 0);
for i in 0..128u32 {
@ -106,7 +106,7 @@ fn lru_works() {
#[test]
fn lru_doesnt_break_volatile_queries() {
let mut db = DatabaseImpl::default();
let mut db = Database::default();
// Create all inputs first, so that there are no revision changes among calls to `get_volatile`
let inputs: Vec<MyInput> = (0..128usize)
@ -126,7 +126,7 @@ fn lru_doesnt_break_volatile_queries() {
#[test]
fn lru_can_be_changed_at_runtime() {
let mut db = DatabaseImpl::default();
let mut db = Database::default();
assert_eq!(load_n_potatoes(), 0);
let inputs: Vec<(u32, MyInput)> = (0..128).map(|i| (i, MyInput::new(&mut db, i))).collect();
@ -169,7 +169,7 @@ fn lru_can_be_changed_at_runtime() {
#[test]
fn lru_keeps_dependency_info() {
let mut db = DatabaseImpl::default();
let mut db = Database::default();
let capacity = 32;
// Invoke `get_hot_potato2` 33 times. This will (in turn) invoke
@ -183,8 +183,7 @@ fn lru_keeps_dependency_info() {
assert_eq!(x as usize, i);
}
db.salsa_runtime_mut()
.synthetic_write(salsa::Durability::HIGH);
db.synthetic_write(salsa::Durability::HIGH);
// We want to test that calls to `get_hot_potato2` are still considered
// clean. Check that no new executions occur as we go here.