mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-02-02 09:46:06 +00:00
Preserve durability when setting a new input-value
This commit is contained in:
parent
05c7fbea45
commit
c4fee35157
3 changed files with 40 additions and 6 deletions
|
@ -116,14 +116,14 @@ impl<C: Configuration> IngredientImpl<C> {
|
||||||
/// * `runtime`, the salsa runtiem
|
/// * `runtime`, the salsa runtiem
|
||||||
/// * `id`, id of the input struct
|
/// * `id`, id of the input struct
|
||||||
/// * `field_index`, index of the field that will be changed
|
/// * `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`
|
/// * `setter`, function that modifies the fields tuple; should only modify the element for `field_index`
|
||||||
pub fn set_field<R>(
|
pub fn set_field<R>(
|
||||||
&mut self,
|
&mut self,
|
||||||
runtime: &mut Runtime,
|
runtime: &mut Runtime,
|
||||||
id: C::Struct,
|
id: C::Struct,
|
||||||
field_index: usize,
|
field_index: usize,
|
||||||
durability: Durability,
|
durability: Option<Durability>,
|
||||||
setter: impl FnOnce(&mut C::Fields) -> R,
|
setter: impl FnOnce(&mut C::Fields) -> R,
|
||||||
) -> R {
|
) -> R {
|
||||||
let id: Id = id.as_id();
|
let id: Id = id.as_id();
|
||||||
|
@ -134,7 +134,7 @@ impl<C: Configuration> IngredientImpl<C> {
|
||||||
runtime.report_tracked_write(stamp.durability);
|
runtime.report_tracked_write(stamp.durability);
|
||||||
}
|
}
|
||||||
|
|
||||||
stamp.durability = durability;
|
stamp.durability = durability.unwrap_or(stamp.durability);
|
||||||
stamp.changed_at = runtime.current_revision();
|
stamp.changed_at = runtime.current_revision();
|
||||||
setter(&mut r.fields)
|
setter(&mut r.fields)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub struct SetterImpl<'setter, C: Configuration, S, F> {
|
||||||
runtime: &'setter mut Runtime,
|
runtime: &'setter mut Runtime,
|
||||||
id: C::Struct,
|
id: C::Struct,
|
||||||
ingredient: &'setter mut IngredientImpl<C>,
|
ingredient: &'setter mut IngredientImpl<C>,
|
||||||
durability: Durability,
|
durability: Option<Durability>,
|
||||||
field_index: usize,
|
field_index: usize,
|
||||||
setter: S,
|
setter: S,
|
||||||
phantom: PhantomData<fn(F)>,
|
phantom: PhantomData<fn(F)>,
|
||||||
|
@ -38,7 +38,7 @@ where
|
||||||
id,
|
id,
|
||||||
field_index,
|
field_index,
|
||||||
ingredient,
|
ingredient,
|
||||||
durability: Durability::LOW,
|
durability: None,
|
||||||
setter,
|
setter,
|
||||||
phantom: PhantomData,
|
phantom: PhantomData,
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ where
|
||||||
type FieldTy = F;
|
type FieldTy = F;
|
||||||
|
|
||||||
fn with_durability(mut self, durability: Durability) -> Self {
|
fn with_durability(mut self, durability: Durability) -> Self {
|
||||||
self.durability = durability;
|
self.durability = Some(durability);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
34
tests/input_setter_preserves_durability.rs
Normal file
34
tests/input_setter_preserves_durability.rs
Normal file
|
@ -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
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in a new issue