fix(fmt_index): impl for accumulator

This commit is contained in:
zjp 2022-08-22 10:04:47 +08:00
parent 67fd9792f0
commit af747c1aca
2 changed files with 30 additions and 24 deletions

View file

@ -48,7 +48,7 @@ fn accumulator_contents(
let struct_ty = &parse_quote! {#struct_name};
let inherent_impl = inherent_impl(args, struct_ty, data_ty);
let ingredients_for_impl = ingredients_for_impl(args, struct_ty, data_ty);
let ingredients_for_impl = ingredients_for_impl(args, struct_name, data_ty);
let struct_item_out = struct_item_out(args, struct_item, data_ty);
let accumulator_impl = accumulator_impl(args, struct_ty, data_ty);
@ -61,23 +61,20 @@ fn accumulator_contents(
}
fn data_ty(struct_item: &syn::ItemStruct) -> syn::Result<&syn::Type> {
match &struct_item.fields {
syn::Fields::Unnamed(fields) => {
if fields.unnamed.len() != 1 {
return Err(syn::Error::new(
struct_item.ident.span(),
"accumulator structs should have only one anonymous field",
));
} else {
Ok(&fields.unnamed[0].ty)
}
}
_ => {
return Err(syn::Error::new(
if let syn::Fields::Unnamed(fields) = &struct_item.fields {
if fields.unnamed.len() != 1 {
Err(syn::Error::new(
struct_item.ident.span(),
"accumulator structs should have only one anonymous field",
));
))
} else {
Ok(&fields.unnamed[0].ty)
}
} else {
Err(syn::Error::new(
struct_item.ident.span(),
"accumulator structs should have only one anonymous field",
))
}
}
@ -109,10 +106,15 @@ fn inherent_impl(args: &Args, struct_ty: &syn::Type, data_ty: &syn::Type) -> syn
}
}
fn ingredients_for_impl(args: &Args, struct_ty: &syn::Type, data_ty: &syn::Type) -> syn::ItemImpl {
fn ingredients_for_impl(
args: &Args,
struct_name: &syn::Ident,
data_ty: &syn::Type,
) -> syn::ItemImpl {
let jar_ty = args.jar_ty();
let debug_name = proc_macro2::Literal::string(&struct_name.to_string());
parse_quote! {
impl salsa::storage::IngredientsFor for #struct_ty {
impl salsa::storage::IngredientsFor for #struct_name {
type Ingredients = salsa::accumulator::AccumulatorIngredient<#data_ty>;
type Jar = #jar_ty;
@ -130,7 +132,7 @@ fn ingredients_for_impl(args: &Args, struct_ty: &syn::Type, data_ty: &syn::Type)
<_ as salsa::storage::HasIngredientsFor<Self>>::ingredient_mut(jar)
},
);
salsa::accumulator::AccumulatorIngredient::new(index)
salsa::accumulator::AccumulatorIngredient::new(index, #debug_name)
}
}
}

View file

@ -1,9 +1,11 @@
//! Basic test of accumulator functionality.
use std::fmt;
use crate::{
cycle::CycleRecoveryStrategy,
hash::FxDashMap,
ingredient::{Ingredient, IngredientRequiresReset},
ingredient::{Ingredient, IngredientRequiresReset, fmt_index},
key::DependencyIndex,
runtime::local_state::QueryOrigin,
storage::HasJar,
@ -21,6 +23,7 @@ pub trait Accumulator {
pub struct AccumulatorIngredient<Data: Clone> {
index: IngredientIndex,
map: FxDashMap<DatabaseKeyIndex, AccumulatedValues<Data>>,
debug_name: &'static str,
}
struct AccumulatedValues<Data> {
@ -29,10 +32,11 @@ struct AccumulatedValues<Data> {
}
impl<Data: Clone> AccumulatorIngredient<Data> {
pub fn new(index: IngredientIndex) -> Self {
pub fn new(index: IngredientIndex, debug_name: &'static str) -> Self {
Self {
map: FxDashMap::default(),
index,
debug_name
}
}
@ -151,10 +155,10 @@ where
fn fmt_index(
&self,
_index: Option<crate::Id>,
_fmt: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
todo!()
index: Option<crate::Id>,
fmt: &mut fmt::Formatter<'_>,
) -> fmt::Result {
fmt_index(self.debug_name, index, fmt)
}
}