From 54c9586b45a7a3febc5e6f504e4eb55d3ac4e96f Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 6 May 2024 05:49:46 -0400 Subject: [PATCH] move interned-specific fns out of salsa struct Salsa struct is already a grab-bag, best to keep it to shared functionality. --- components/salsa-2022-macros/src/interned.rs | 34 +++++++++++++++++ .../salsa-2022-macros/src/salsa_struct.rs | 38 ++----------------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/components/salsa-2022-macros/src/interned.rs b/components/salsa-2022-macros/src/interned.rs index 498ae652..ecfcdbc6 100644 --- a/components/salsa-2022-macros/src/interned.rs +++ b/components/salsa-2022-macros/src/interned.rs @@ -79,6 +79,40 @@ impl InternedStruct { Ok(()) } + /// The name of the "data" struct (this comes from the `data = Foo` option or, + /// if that is not provided, by concatenating `Data` to the name of the struct). + fn data_ident(&self) -> syn::Ident { + match &self.args().data { + Some(d) => d.clone(), + None => syn::Ident::new( + &format!("__{}Data", self.the_ident()), + self.the_ident().span(), + ), + } + } + + /// Generates the `struct FooData` struct (or enum). + /// This type inherits all the attributes written by the user. + /// + /// When using named fields, we synthesize the struct and field names. + /// + /// When no named fields are available, copy the existing type. + fn data_struct(&self) -> syn::ItemStruct { + let ident = self.data_ident(); + let visibility = self.visibility(); + let all_field_names = self.all_field_names(); + let all_field_tys = self.all_field_tys(); + parse_quote_spanned! { ident.span() => + /// Internal struct used for interned item + #[derive(Eq, PartialEq, Hash, Clone)] + #visibility struct #ident { + #( + #all_field_names: #all_field_tys, + )* + } + } + } + /// If this is an interned struct, then generate methods to access each field, /// as well as a `new` method. fn inherent_impl_for_named_fields(&self) -> syn::ItemImpl { diff --git a/components/salsa-2022-macros/src/salsa_struct.rs b/components/salsa-2022-macros/src/salsa_struct.rs index f4241ef2..1e3c2a7e 100644 --- a/components/salsa-2022-macros/src/salsa_struct.rs +++ b/components/salsa-2022-macros/src/salsa_struct.rs @@ -84,6 +84,10 @@ impl SalsaStruct { }) } + pub(crate) fn args(&self) -> &Options { + &self.args + } + pub(crate) fn require_no_generics(&self) -> syn::Result<()> { if let Some(param) = self.struct_item.generics.params.iter().next() { return Err(syn::Error::new_spanned( @@ -268,18 +272,6 @@ impl SalsaStruct { } } - /// The name of the "data" struct (this comes from the `data = Foo` option or, - /// if that is not provided, by concatenating `Data` to the name of the struct). - pub(crate) fn data_ident(&self) -> syn::Ident { - match &self.args.data { - Some(d) => d.clone(), - None => syn::Ident::new( - &format!("__{}Data", self.the_ident()), - self.the_ident().span(), - ), - } - } - /// Create "the struct" whose field is an id. /// This is the struct the user will refernece, but only if there /// are no lifetimes. @@ -346,28 +338,6 @@ impl SalsaStruct { } } - /// Generates the `struct FooData` struct (or enum). - /// This type inherits all the attributes written by the user. - /// - /// When using named fields, we synthesize the struct and field names. - /// - /// When no named fields are available, copy the existing type. - pub(crate) fn data_struct(&self) -> syn::ItemStruct { - let ident = self.data_ident(); - let visibility = self.visibility(); - let all_field_names = self.all_field_names(); - let all_field_tys = self.all_field_tys(); - parse_quote_spanned! { ident.span() => - /// Internal struct used for interned item - #[derive(Eq, PartialEq, Hash, Clone)] - #visibility struct #ident { - #( - #all_field_names: #all_field_tys, - )* - } - } - } - /// Returns the visibility of this item pub(crate) fn visibility(&self) -> &syn::Visibility { &self.struct_item.vis