add cycle_recovery_strategy function on database

This commit is contained in:
Niko Matsakis 2021-10-25 05:26:14 -04:00
parent e49088644d
commit fc826b0689
3 changed files with 47 additions and 0 deletions

View file

@ -106,6 +106,7 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
// ANCHOR:DatabaseOps
let mut fmt_ops = proc_macro2::TokenStream::new();
let mut maybe_changed_ops = proc_macro2::TokenStream::new();
let mut cycle_recovery_strategy_ops = proc_macro2::TokenStream::new();
let mut for_each_ops = proc_macro2::TokenStream::new();
for ((QueryGroup { group_path }, group_storage), group_index) in query_groups
.iter()
@ -126,6 +127,13 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
storage.maybe_changed_since(self, input, revision)
}
});
cycle_recovery_strategy_ops.extend(quote! {
#group_index => {
let storage: &#group_storage =
<Self as salsa::plumbing::HasQueryGroup<#group_path>>::group_storage(self);
storage.cycle_recovery_strategy(self, input)
}
});
for_each_ops.extend(quote! {
let storage: &#group_storage =
<Self as salsa::plumbing::HasQueryGroup<#group_path>>::group_storage(self);
@ -168,6 +176,16 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
}
}
fn cycle_recovery_strategy(
&self,
input: salsa::DatabaseKeyIndex,
) -> salsa::plumbing::CycleRecoveryStrategy {
match input.group_index() {
#cycle_recovery_strategy_ops
i => panic!("salsa: invalid group index {}", i)
}
}
fn for_each_query(
&self,
mut op: &mut dyn FnMut(&dyn salsa::plumbing::QueryStorageMassOps),

View file

@ -539,6 +539,17 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
});
}
let mut cycle_recovery_strategy_ops = proc_macro2::TokenStream::new();
for (Query { fn_name, .. }, query_index) in non_transparent_queries().zip(0_u16..) {
cycle_recovery_strategy_ops.extend(quote! {
#query_index => {
salsa::plumbing::QueryStorageOps::cycle_recovery_strategy(
&*self.#fn_name
)
}
});
}
let mut for_each_ops = proc_macro2::TokenStream::new();
for Query { fn_name, .. } in non_transparent_queries() {
for_each_ops.extend(quote! {
@ -591,6 +602,17 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
}
}
#trait_vis fn cycle_recovery_strategy(
&self,
db: &(#dyn_db + '_),
input: salsa::DatabaseKeyIndex,
) -> salsa::plumbing::CycleRecoveryStrategy {
match input.query_index() {
#cycle_recovery_strategy_ops
i => panic!("salsa: impossible query index {}", i),
}
}
#trait_vis fn for_each_query(
&self,
_runtime: &salsa::Runtime,

View file

@ -56,6 +56,9 @@ pub trait DatabaseOps {
/// True if the computed value for `input` may have changed since `revision`.
fn maybe_changed_since(&self, input: DatabaseKeyIndex, revision: Revision) -> bool;
/// Find the `CycleRecoveryStrategy` for a given input.
fn cycle_recovery_strategy(&self, input: DatabaseKeyIndex) -> CycleRecoveryStrategy;
/// Executes the callback for each kind of query.
fn for_each_query(&self, op: &mut dyn FnMut(&dyn QueryStorageMassOps));
}
@ -172,6 +175,10 @@ where
revision: Revision,
) -> bool;
fn cycle_recovery_strategy(&self) -> CycleRecoveryStrategy {
Self::CYCLE_STRATEGY
}
/// Execute the query, returning the result (often, the result
/// will be memoized). This is the "main method" for
/// queries.