Skip to content

Commit

Permalink
Merge pull request #559 from MichaReiser/setter-preserve-durability
Browse files Browse the repository at this point in the history
Preserve durability when setting a new input-value
  • Loading branch information
nikomatsakis authored Aug 8, 2024
2 parents 04a1e32 + c4fee35 commit 0c1d8b6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ impl<C: Configuration> IngredientImpl<C> {
/// * `runtime`, the salsa runtiem
/// * `id`, id of the input struct
/// * `field_index`, index of the field that will be changed
/// * `durability`, durability of the new value
/// * `durability`, durability of the new value. If omitted, uses the durability of the previous value.
/// * `setter`, function that modifies the fields tuple; should only modify the element for `field_index`
pub fn set_field<R>(
&mut self,
runtime: &mut Runtime,
id: C::Struct,
field_index: usize,
durability: Durability,
durability: Option<Durability>,
setter: impl FnOnce(&mut C::Fields) -> R,
) -> R {
let id: Id = id.as_id();
Expand All @@ -134,7 +134,7 @@ impl<C: Configuration> IngredientImpl<C> {
runtime.report_tracked_write(stamp.durability);
}

stamp.durability = durability;
stamp.durability = durability.unwrap_or(stamp.durability);
stamp.changed_at = runtime.current_revision();
setter(&mut r.fields)
}
Expand Down
6 changes: 3 additions & 3 deletions src/input/setter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct SetterImpl<'setter, C: Configuration, S, F> {
runtime: &'setter mut Runtime,
id: C::Struct,
ingredient: &'setter mut IngredientImpl<C>,
durability: Durability,
durability: Option<Durability>,
field_index: usize,
setter: S,
phantom: PhantomData<fn(F)>,
Expand All @@ -38,7 +38,7 @@ where
id,
field_index,
ingredient,
durability: Durability::LOW,
durability: None,
setter,
phantom: PhantomData,
}
Expand All @@ -53,7 +53,7 @@ where
type FieldTy = F;

fn with_durability(mut self, durability: Durability) -> Self {
self.durability = durability;
self.durability = Some(durability);
self
}

Expand Down
34 changes: 34 additions & 0 deletions tests/input_setter_preserves_durability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use test_log::test;

use salsa::plumbing::ZalsaDatabase;
use salsa::{Durability, Setter};

#[salsa::input]
struct MyInput {
required_field: bool,

#[default]
optional_field: usize,
}

#[test]
fn execute() {
let mut db = salsa::DatabaseImpl::new();

let input = MyInput::builder(true)
.required_field_durability(Durability::HIGH)
.new(&db);

// Change the field value. It should preserve high durability.
input.set_required_field(&mut db).to(false);

let last_high_revision = db.zalsa().last_changed_revision(Durability::HIGH);

// Changing the value again should **again** dump the high durability revision.
input.set_required_field(&mut db).to(false);

assert_ne!(
db.zalsa().last_changed_revision(Durability::HIGH),
last_high_revision
);
}

0 comments on commit 0c1d8b6

Please sign in to comment.