From 9bd9f8b202f2fbc8586668e7c40ce332a9d1e8ce Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 30 Jun 2020 10:07:00 +0000 Subject: [PATCH] use `new` with index to create group storage --- .../salsa-macros/src/database_storage.rs | 20 +++++++++++++++++-- components/salsa-macros/src/query_group.rs | 5 ++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/components/salsa-macros/src/database_storage.rs b/components/salsa-macros/src/database_storage.rs index 5615848b..2b45c837 100644 --- a/components/salsa-macros/src/database_storage.rs +++ b/components/salsa-macros/src/database_storage.rs @@ -46,12 +46,14 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream { // For each query group `foo::MyGroup` create a link to its // `foo::MyGroupGroupStorage` let mut storage_fields = proc_macro2::TokenStream::new(); + let mut storage_initializers = proc_macro2::TokenStream::new(); let mut has_group_impls = proc_macro2::TokenStream::new(); - for (((query_group, group_name_snake), group_storage), group_key) in query_groups + for ((((query_group, group_name_snake), group_storage), group_key), group_index) in query_groups .iter() .zip(&query_group_names_snake) .zip(&query_group_storage_names) .zip(&query_group_key_names) + .zip(0_usize..) { let group_path = &query_group.group_path; let group_name = query_group.name(); @@ -61,6 +63,13 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream { storage_fields.extend(quote! { #group_name_snake: #group_storage, }); + + // rewrite the last identifier (`MyGroup`, above) to + // (e.g.) `MyGroupGroupStorage`. + storage_initializers.extend(quote! { + #group_name_snake: #group_storage::new(#group_index), + }); + // ANCHOR:HasQueryGroup has_group_impls.extend(quote! { impl salsa::plumbing::HasQueryGroup<#group_path> for #database_name { @@ -81,11 +90,18 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream { // create group storage wrapper struct output.extend(quote! { - #[derive(Default)] #[doc(hidden)] #visibility struct __SalsaDatabaseStorage { #storage_fields } + + impl Default for __SalsaDatabaseStorage { + fn default() -> Self { + Self { + #storage_initializers + } + } + } }); // create query database_key wrapper struct diff --git a/components/salsa-macros/src/query_group.rs b/components/salsa-macros/src/query_group.rs index e813e91c..e03243ef 100644 --- a/components/salsa-macros/src/query_group.rs +++ b/components/salsa-macros/src/query_group.rs @@ -478,14 +478,13 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream #storage_fields } - impl Default for #group_storage + impl #group_storage where DB__: #trait_name + #requires, DB__: salsa::plumbing::HasQueryGroup<#group_struct>, DB__: salsa::Database, { - #[inline] - fn default() -> Self { + #trait_vis fn new(group_index: usize) -> Self { #group_storage { #storage_defaults }