feat(literal): a helper to convert Ident to Literal

This commit is contained in:
zjp 2022-08-22 20:22:57 +08:00
parent d835b0782c
commit 4966acbc64
5 changed files with 13 additions and 14 deletions

View file

@ -112,7 +112,7 @@ fn ingredients_for_impl(
data_ty: &syn::Type,
) -> syn::ItemImpl {
let jar_ty = args.jar_ty();
let debug_name = proc_macro2::Literal::string(&struct_name.to_string());
let debug_name = crate::literal(struct_name);
parse_quote! {
impl salsa::storage::IngredientsFor for #struct_name {
type Ingredients = salsa::accumulator::AccumulatorIngredient<#data_ty>;

View file

@ -130,17 +130,14 @@ impl InputStruct {
/// The entity's ingredients include both the main entity ingredient along with a
/// function ingredient for each of the value fields.
fn input_ingredients(&self) -> syn::ItemImpl {
use crate::literal;
let ident = self.id_ident();
let field_ty = self.all_field_tys();
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_struct = Literal::string(&self.id_ident().to_string());
let debug_name_fields: Vec<_> = self
.all_field_names()
.into_iter()
.map(|ident| Literal::string(&ident.to_string()))
.collect();
let debug_name_struct = literal(self.id_ident());
let debug_name_fields: Vec<_> = self.all_field_names().into_iter().map(literal).collect();
parse_quote! {
impl salsa::storage::IngredientsFor for #ident {

View file

@ -25,6 +25,11 @@ macro_rules! parse_quote_spanned {
}
}
/// Convert a single Ident to Literal: useful when &'static str is needed.
pub(crate) fn literal(ident: &proc_macro2::Ident) -> proc_macro2::Literal {
proc_macro2::Literal::string(&ident.to_string())
}
mod accumulator;
mod configuration;
mod db;

View file

@ -243,7 +243,7 @@ fn ingredients_for_impl(
let lru = args.lru.unwrap_or(0);
// get the name of the function as a string literal
let debug_name = Literal::string(&item_fn.sig.ident.to_string());
let debug_name = crate::literal(&item_fn.sig.ident);
parse_quote! {
impl salsa::storage::IngredientsFor for #config_ty {

View file

@ -153,18 +153,15 @@ impl TrackedStruct {
/// The tracked struct's ingredients include both the main tracked struct ingredient along with a
/// function ingredient for each of the value fields.
fn tracked_struct_ingredients(&self, config_structs: &[syn::ItemStruct]) -> syn::ItemImpl {
use crate::literal;
let ident = self.id_ident();
let jar_ty = self.jar_ty();
let id_field_tys: Vec<&syn::Type> = self.id_fields().map(SalsaField::ty).collect();
let value_field_indices: Vec<Literal> = self.value_field_indices();
let tracked_struct_index: Literal = self.tracked_struct_index();
let config_struct_names = config_structs.iter().map(|s| &s.ident);
let debug_name_struct = Literal::string(&self.id_ident().to_string());
let debug_name_fields: Vec<_> = self
.all_field_names()
.into_iter()
.map(|ident| Literal::string(&ident.to_string()))
.collect();
let debug_name_struct = literal(self.id_ident());
let debug_name_fields: Vec<_> = self.all_field_names().into_iter().map(literal).collect();
parse_quote! {
impl salsa::storage::IngredientsFor for #ident {