mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-15 01:39:25 +00:00
Add InputFieldIngredient
This commit is contained in:
parent
648248bd39
commit
da43bd2ba6
2 changed files with 68 additions and 0 deletions
67
components/salsa-2022/src/input_field.rs
Normal file
67
components/salsa-2022/src/input_field.rs
Normal 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
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue