Respct no_eq attribute for tracked functions

This commit is contained in:
Micha Reiser 2024-07-20 08:47:51 +02:00
parent 1c69d3ba7b
commit ba169ef039
No known key found for this signature in database
3 changed files with 84 additions and 1 deletions

View file

@ -43,6 +43,9 @@ macro_rules! setup_tracked_fn {
// If true, this is specifiable.
is_specifiable: $is_specifiable:tt,
// If true, don't backdate the value when the new value compares equal to the old value.
no_eq: $no_eq:tt,
// If true, the input needs an interner (because it has >1 argument).
needs_interner: $needs_interner:tt,
@ -162,7 +165,13 @@ macro_rules! setup_tracked_fn {
old_value: &Self::Output<'_>,
new_value: &Self::Output<'_>,
) -> bool {
$zalsa::should_backdate_value(old_value, new_value)
$zalsa::macro_if! {
if $no_eq {
false
} else {
$zalsa::should_backdate_value(old_value, new_value)
}
}
}
fn execute<$db_lt>($db: &$db_lt Self::DbView, ($($input_id),*): ($($input_ty),*)) -> Self::Output<$db_lt> {

View file

@ -69,6 +69,7 @@ impl Macro {
let output_ty = self.output_ty(&db_lt, &item)?;
let (cycle_recovery_fn, cycle_recovery_strategy) = self.cycle_recovery();
let is_specifiable = self.args.specify.is_some();
let no_eq = self.args.no_eq.is_some();
let mut inner_fn = item.clone();
inner_fn.vis = syn::Visibility::Inherited;
@ -127,6 +128,7 @@ impl Macro {
cycle_recovery_fn: #cycle_recovery_fn,
cycle_recovery_strategy: #cycle_recovery_strategy,
is_specifiable: #is_specifiable,
no_eq: #no_eq,
needs_interner: #needs_interner,
lru: #lru,
return_ref: #return_ref,

72
tests/tracked_fn_no_eq.rs Normal file
View file

@ -0,0 +1,72 @@
mod common;
use common::{HasLogger, Logger};
use expect_test::expect;
use salsa::Setter as _;
#[salsa::db]
trait Db: salsa::Database + HasLogger {}
#[salsa::input]
struct Input {
number: i16,
}
#[salsa::tracked(no_eq)]
fn abs_float(db: &dyn Db, input: Input) -> f32 {
let number = input.number(db);
db.push_log(format!("abs_float({number})"));
number.abs() as f32
}
#[salsa::tracked]
fn derived(db: &dyn Db, input: Input) -> u32 {
let x = abs_float(db, input);
db.push_log("derived".to_string());
x as u32
}
#[salsa::db]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
logger: Logger,
}
impl HasLogger for Database {
fn logger(&self) -> &Logger {
&self.logger
}
}
#[salsa::db]
impl salsa::Database for Database {}
#[salsa::db]
impl Db for Database {}
#[test]
fn invoke() {
let mut db = Database::default();
let input = Input::new(&db, 5);
let x = derived(&db, input);
assert_eq!(x, 5);
input.set_number(&mut db).to(-5);
// Derived should re-execute even the result of `abs_float` is the same.
let x = derived(&db, input);
assert_eq!(x, 5);
db.assert_logs(expect![[r#"
[
"abs_float(5)",
"derived",
"abs_float(-5)",
"derived",
]"#]]);
}