From da43bd2ba6cf2c1b426475b47500263a5894871b Mon Sep 17 00:00:00 2001 From: Mihail Mihov Date: Thu, 11 Aug 2022 20:24:32 +0300 Subject: [PATCH] Add InputFieldIngredient --- components/salsa-2022/src/input_field.rs | 67 ++++++++++++++++++++++++ components/salsa-2022/src/lib.rs | 1 + 2 files changed, 68 insertions(+) create mode 100644 components/salsa-2022/src/input_field.rs diff --git a/components/salsa-2022/src/input_field.rs b/components/salsa-2022/src/input_field.rs new file mode 100644 index 0000000..1229115 --- /dev/null +++ b/components/salsa-2022/src/input_field.rs @@ -0,0 +1,67 @@ +use std::hash::Hash; +use rustc_hash::FxHashMap; +use crate::{Durability, IngredientIndex, Revision, Runtime}; +use crate::cycle::CycleRecoveryStrategy; +use crate::ingredient::Ingredient; +use crate::key::DependencyIndex; +use crate::runtime::local_state::QueryInputs; +use crate::runtime::StampedValue; + +pub struct InputFieldIngredient { + index: IngredientIndex, + map: FxHashMap> +} + +impl InputFieldIngredient +where K: Eq + Hash +{ + pub fn new(index: IngredientIndex) -> Self { + Self { + index, + map: Default::default(), + } + } + + pub fn store( + &mut self, + runtime: &mut Runtime, + key: K, + value: F, + durability: Durability, + ) -> Option { + let revision = runtime.current_revision(); + let stamped_value = StampedValue { + value, + durability, + changed_at: revision + }; + + if let Some(old_value) = self.map.insert(key, stamped_value) { + Some(old_value.value) + } else { + None + } + } + + pub fn fetch( + &self, + key: K, + ) -> &F { + &self.map.get(&key).unwrap().value + } +} + +impl Ingredient for InputFieldIngredient +{ + fn cycle_recovery_strategy(&self) -> CycleRecoveryStrategy { + CycleRecoveryStrategy::Panic + } + + fn maybe_changed_after(&self, _db: &DB, _input: DependencyIndex, _revision: Revision) -> bool { + false + } + + fn inputs(&self, _key_index: crate::Id) -> Option { + None + } +} diff --git a/components/salsa-2022/src/lib.rs b/components/salsa-2022/src/lib.rs index fe903c1..1558cbb 100644 --- a/components/salsa-2022/src/lib.rs +++ b/components/salsa-2022/src/lib.rs @@ -10,6 +10,7 @@ pub mod hash; pub mod id; pub mod ingredient; pub mod input; +pub mod input_field; pub mod interned; pub mod jar; pub mod key;