feat(fmt_index): added in Ingredient trait

This commit is contained in:
zjp 2022-08-21 23:55:29 +08:00
parent c84f88d23a
commit 70b0340b81
11 changed files with 77 additions and 4 deletions

View file

@ -159,6 +159,10 @@ fn has_jars_dyn_impl(input: &syn::ItemStruct, storage: &syn::Ident) -> syn::Item
let ingredient = self.#storage.ingredient(ingredient);
ingredient.salsa_struct_deleted(self, id);
}
fn fmt_index(&self, index: salsa::DependencyIndex, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let ingredient = self.#storage.ingredient(ingredient);
ingredient.fmt_index(self, index, fmt)
}
}
}
}

View file

@ -135,6 +135,7 @@ impl InputStruct {
let jar_ty = self.jar_ty();
let all_field_indices: Vec<Literal> = self.all_field_indices();
let input_index: Literal = self.input_index();
let debug_name = Literal::string(&format!("{}", self.id_ident()));
parse_quote! {
impl salsa::storage::IngredientsFor for #ident {
@ -183,7 +184,7 @@ impl InputStruct {
&mut ingredients.#input_index
},
);
salsa::input::InputIngredient::new(index)
salsa::input::InputIngredient::new(index, #debug_name)
},
)
}

View file

@ -242,6 +242,9 @@ fn ingredients_for_impl(
// set 0 as default to disable LRU
let lru = args.lru.unwrap_or(0);
// get the name of the function as a string literal
let debug_name = Literal::string(&format!("{}", item_fn.sig.ident));
parse_quote! {
impl salsa::storage::IngredientsFor for #config_ty {
type Ingredients = Self;
@ -268,7 +271,7 @@ fn ingredients_for_impl(
<_ as salsa::storage::HasIngredientsFor<Self::Ingredients>>::ingredient_mut(jar);
&mut ingredients.function
});
let ingredient = salsa::function::FunctionIngredient::new(index);
let ingredient = salsa::function::FunctionIngredient::new(index, #debug_name);
ingredient.set_capacity(#lru);
ingredient
}

View file

@ -148,6 +148,14 @@ where
fn salsa_struct_deleted(&self, _db: &DB, _id: crate::Id) {
panic!("unexpected call: accumulator is not registered as a dependent fn");
}
fn fmt_index(
&self,
_index: Option<crate::Id>,
_fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
todo!()
}
}
impl<Data> IngredientRequiresReset for AccumulatorIngredient<Data>

View file

@ -73,6 +73,8 @@ pub struct FunctionIngredient<C: Configuration> {
/// Set to true once we invoke `register_dependent_fn` for `C::SalsaStruct`.
/// Prevents us from registering more than once.
registered: AtomicCell<bool>,
debug_name: &'static str,
}
pub trait Configuration {
@ -139,7 +141,7 @@ impl<C> FunctionIngredient<C>
where
C: Configuration,
{
pub fn new(index: IngredientIndex) -> Self {
pub fn new(index: IngredientIndex, debug_name: &'static str) -> Self {
Self {
index,
memo_map: memo::MemoMap::default(),
@ -147,6 +149,7 @@ where
sync_map: Default::default(),
deleted_entries: Default::default(),
registered: Default::default(),
debug_name,
}
}
@ -273,6 +276,14 @@ where
}
}
}
fn fmt_index(
&self,
_index: Option<crate::Id>,
_fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
todo!()
}
}
impl<C> IngredientRequiresReset for FunctionIngredient<C>

View file

@ -59,6 +59,12 @@ pub trait Ingredient<DB: ?Sized> {
/// **Important:** to actually receive resets, the ingredient must set
/// [`IngredientRequiresReset::RESET_ON_NEW_REVISION`] to true.
fn reset_for_new_revision(&mut self);
fn fmt_index(
&self,
index: Option<crate::Id>,
fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result;
}
/// Defines a const indicating if an ingredient needs to be reset each round.

View file

@ -15,6 +15,7 @@ where
{
ingredient_index: IngredientIndex,
counter: u32,
debug_name: &'static str,
_phantom: std::marker::PhantomData<Id>,
}
@ -22,10 +23,11 @@ impl<Id> InputIngredient<Id>
where
Id: InputId,
{
pub fn new(index: IngredientIndex) -> Self {
pub fn new(index: IngredientIndex, debug_name: &'static str) -> Self {
Self {
ingredient_index: index,
counter: Default::default(),
debug_name,
_phantom: std::marker::PhantomData,
}
}
@ -95,6 +97,14 @@ where
"unexpected call: input ingredients do not register for salsa struct deletion events"
);
}
fn fmt_index(
&self,
_index: Option<crate::Id>,
_fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
todo!()
}
}
impl<Id> IngredientRequiresReset for InputIngredient<Id>

View file

@ -113,6 +113,14 @@ where
fn reset_for_new_revision(&mut self) {
panic!("unexpected call: input fields don't register for resets");
}
fn fmt_index(
&self,
_index: Option<crate::Id>,
_fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
todo!()
}
}
impl<K, F> IngredientRequiresReset for InputFieldIngredient<K, F>

View file

@ -234,6 +234,14 @@ where
fn salsa_struct_deleted(&self, _db: &DB, _id: crate::Id) {
panic!("unexpected call: interned ingredients do not register for salsa struct deletion events");
}
fn fmt_index(
&self,
_index: Option<crate::Id>,
_fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
todo!()
}
}
impl<Id, Data> IngredientRequiresReset for InternedIngredient<Id, Data>

View file

@ -220,6 +220,12 @@ pub trait HasJarsDyn {
/// as a dependent function using
/// [`SalsaStructInDb::register_dependent_fn`](`crate::salsa_struct::SalsaStructInDb::register_dependent_fn`).
fn salsa_struct_deleted(&self, ingredient: IngredientIndex, id: Id);
fn fmt_index(
&self,
index: DependencyIndex,
fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result;
}
// ANCHOR_END: HasJarsDyn

View file

@ -175,6 +175,14 @@ where
fn salsa_struct_deleted(&self, _db: &DB, _id: crate::Id) {
panic!("unexpected call: interned ingredients do not register for salsa struct deletion events");
}
fn fmt_index(
&self,
_index: Option<crate::Id>,
_fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
todo!()
}
}
impl<Id, Data> IngredientRequiresReset for TrackedStructIngredient<Id, Data>