modify public API to set_foo_with_durability

This commit is contained in:
Niko Matsakis 2019-06-25 17:54:28 -04:00
parent 09c9bd9761
commit a0a6bac5af
5 changed files with 45 additions and 26 deletions

View file

@ -223,8 +223,8 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
// For input queries, we need `set_foo` etc
if let QueryStorage::Input = query.storage {
let set_fn_name = Ident::new(&format!("set_{}", fn_name), fn_name.span());
let set_constant_fn_name =
Ident::new(&format!("set_constant_{}", fn_name), fn_name.span());
let set_with_durability_fn_name =
Ident::new(&format!("set_{}_with_durability", fn_name), fn_name.span());
let set_fn_docs = format!(
"
@ -259,7 +259,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
# [doc = #set_constant_fn_docs]
fn #set_constant_fn_name(&mut self, #(#key_names: #keys,)* value__: #value);
fn #set_with_durability_fn_name(&mut self, #(#key_names: #keys,)* value__: #value, durability__: salsa::Durability);
});
query_fn_definitions.extend(quote! {
@ -267,8 +267,8 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
<Self as salsa::plumbing::GetQueryTable<#qt>>::get_query_table_mut(self).set((#(#key_names),*), value__)
}
fn #set_constant_fn_name(&mut self, #(#key_names: #keys,)* value__: #value) {
<Self as salsa::plumbing::GetQueryTable<#qt>>::get_query_table_mut(self).set_constant((#(#key_names),*), value__)
fn #set_with_durability_fn_name(&mut self, #(#key_names: #keys,)* value__: #value, durability__: salsa::Durability) {
<Self as salsa::plumbing::GetQueryTable<#qt>>::get_query_table_mut(self).set_with_durability((#(#key_names),*), value__, durability__)
}
});
}

View file

@ -187,10 +187,16 @@ where
self.set_common(db, key, database_key, value, Durability::LOW);
}
fn set_constant(&self, db: &DB, key: &Q::Key, database_key: &DB::DatabaseKey, value: Q::Value) {
fn set_with_durability(
&self,
db: &DB,
key: &Q::Key,
database_key: &DB::DatabaseKey,
value: Q::Value,
durability: Durability,
) {
log::debug!("{:?}({:?}) = {:?}", Q::default(), key, value);
let durability = db.salsa_runtime().max_durability();
self.set_common(db, key, database_key, value, durability);
}
}

View file

@ -540,12 +540,17 @@ where
/// and cancellation on [the `query_mut` method].
///
/// [the `query_mut` method]: trait.Database#method.query_mut
pub fn set_constant(&self, key: Q::Key, value: Q::Value)
pub fn set_with_durability(&self, key: Q::Key, value: Q::Value, durability: Durability)
where
Q::Storage: plumbing::InputQueryStorageOps<DB, Q>,
{
self.storage
.set_constant(self.db, &key, &self.database_key(&key), value);
self.storage.set_with_durability(
self.db,
&key,
&self.database_key(&key),
value,
durability,
);
}
/// Sets the size of LRU cache of values for this query table.

View file

@ -167,12 +167,13 @@ where
{
fn set(&self, db: &DB, key: &Q::Key, database_key: &DB::DatabaseKey, new_value: Q::Value);
fn set_constant(
fn set_with_durability(
&self,
db: &DB,
key: &Q::Key,
database_key: &DB::DatabaseKey,
new_value: Q::Value,
durability: Durability,
);
}

View file

@ -27,11 +27,11 @@ fn add3(db: &impl ConstantsDatabase, key1: char, key2: char, key3: char) -> usiz
#[test]
fn invalidate_constant() {
let db = &mut TestContextImpl::default();
db.set_constant_input('a', 44);
db.set_constant_input('b', 22);
db.set_input_with_durability('a', 44, Durability::HIGH);
db.set_input_with_durability('b', 22, Durability::HIGH);
assert_eq!(db.add('a', 'b'), 66);
db.set_constant_input('a', 66);
db.set_input_with_durability('a', 66, Durability::HIGH);
assert_eq!(db.add('a', 'b'), 88);
}
@ -44,11 +44,11 @@ fn invalidate_constant_1() {
assert_eq!(db.add('a', 'a'), 88);
// Becomes constant:
db.set_constant_input('a', 44);
db.set_input_with_durability('a', 44, Durability::HIGH);
assert_eq!(db.add('a', 'a'), 88);
// Invalidates:
db.set_constant_input('a', 33);
db.set_input_with_durability('a', 33, Durability::HIGH);
assert_eq!(db.add('a', 'a'), 66);
}
@ -57,8 +57,8 @@ fn invalidate_constant_1() {
#[test]
fn set_after_constant_same_value() {
let db = &mut TestContextImpl::default();
db.set_constant_input('a', 44);
db.set_constant_input('a', 44);
db.set_input_with_durability('a', 44, Durability::HIGH);
db.set_input_with_durability('a', 44, Durability::HIGH);
db.set_input('a', 44);
}
@ -76,8 +76,8 @@ fn not_constant() {
fn durability() {
let db = &mut TestContextImpl::default();
db.set_constant_input('a', 22);
db.set_constant_input('b', 44);
db.set_input_with_durability('a', 22, Durability::HIGH);
db.set_input_with_durability('b', 44, Durability::HIGH);
assert_eq!(db.add('a', 'b'), 66);
assert_eq!(Durability::HIGH, db.query(AddQuery).durability(('a', 'b')));
}
@ -86,7 +86,7 @@ fn durability() {
fn mixed_constant() {
let db = &mut TestContextImpl::default();
db.set_constant_input('a', 22);
db.set_input_with_durability('a', 22, Durability::HIGH);
db.set_input('b', 44);
assert_eq!(db.add('a', 'b'), 66);
assert_eq!(Durability::LOW, db.query(AddQuery).durability(('a', 'b')));
@ -101,13 +101,20 @@ fn becomes_constant_with_change() {
assert_eq!(db.add('a', 'b'), 66);
assert_eq!(Durability::LOW, db.query(AddQuery).durability(('a', 'b')));
db.set_constant_input('a', 23);
db.set_input_with_durability('a', 23, Durability::HIGH);
assert_eq!(db.add('a', 'b'), 67);
assert_eq!(Durability::LOW, db.query(AddQuery).durability(('a', 'b')));
db.set_constant_input('b', 45);
db.set_input_with_durability('b', 45, Durability::HIGH);
assert_eq!(db.add('a', 'b'), 68);
assert_eq!(Durability::HIGH, db.query(AddQuery).durability(('a', 'b')));
db.set_input_with_durability('b', 45, Durability::MEDIUM);
assert_eq!(db.add('a', 'b'), 68);
assert_eq!(
Durability::MEDIUM,
db.query(AddQuery).durability(('a', 'b'))
);
}
// Test a subtle case in which an input changes from constant to
@ -118,9 +125,9 @@ fn becomes_constant_with_change() {
fn constant_to_non_constant() {
let db = &mut TestContextImpl::default();
db.set_constant_input('a', 11);
db.set_constant_input('b', 22);
db.set_constant_input('c', 33);
db.set_input_with_durability('a', 11, Durability::HIGH);
db.set_input_with_durability('b', 22, Durability::HIGH);
db.set_input_with_durability('c', 33, Durability::HIGH);
// Here, `add3` invokes `add`, which yields 33. Both calls are
// constant.