Add InputFieldIngredient

This commit is contained in:
Mihail Mihov 2022-08-11 20:24:32 +03:00
parent 648248bd39
commit da43bd2ba6
2 changed files with 68 additions and 0 deletions

View file

@ -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<K, F> {
index: IngredientIndex,
map: FxHashMap<K, StampedValue<F>>
}
impl<K, F> InputFieldIngredient<K, F>
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<F> {
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<DB: ?Sized, K, F> Ingredient<DB> for InputFieldIngredient<K, F>
{
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<QueryInputs> {
None
}
}

View file

@ -10,6 +10,7 @@ pub mod hash;
pub mod id; pub mod id;
pub mod ingredient; pub mod ingredient;
pub mod input; pub mod input;
pub mod input_field;
pub mod interned; pub mod interned;
pub mod jar; pub mod jar;
pub mod key; pub mod key;