From f5b0ff59d380f915d6c9f9aad6523ed7e9550444 Mon Sep 17 00:00:00 2001 From: XFFXFF <1247714429@qq.com> Date: Wed, 24 Aug 2022 20:01:00 +0800 Subject: [PATCH] add runtime_mut and synthetic_write to HasJarsDyn --- components/salsa-2022-macros/src/db.rs | 9 +++++++++ components/salsa-2022/src/storage.rs | 6 +++++- salsa-2022-tests/tests/lru.rs | 21 ++++++++++----------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/components/salsa-2022-macros/src/db.rs b/components/salsa-2022-macros/src/db.rs index fabb706e..78c20731 100644 --- a/components/salsa-2022-macros/src/db.rs +++ b/components/salsa-2022-macros/src/db.rs @@ -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); + } } } } diff --git a/components/salsa-2022/src/storage.rs b/components/salsa-2022/src/storage.rs index bc5dfc08..3c143f5f 100644 --- a/components/salsa-2022/src/storage.rs +++ b/components/salsa-2022/src/storage.rs @@ -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 { 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 diff --git a/salsa-2022-tests/tests/lru.rs b/salsa-2022-tests/tests/lru.rs index e8186e7d..15ef66ac 100644 --- a/salsa-2022-tests/tests/lru.rs +++ b/salsa-2022-tests/tests/lru.rs @@ -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, 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 = (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.