use new with index to create group storage

This commit is contained in:
Niko Matsakis 2020-06-30 10:07:00 +00:00
parent 0da53151c5
commit 9bd9f8b202
2 changed files with 20 additions and 5 deletions

View file

@ -46,12 +46,14 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
// For each query group `foo::MyGroup` create a link to its // For each query group `foo::MyGroup` create a link to its
// `foo::MyGroupGroupStorage` // `foo::MyGroupGroupStorage`
let mut storage_fields = proc_macro2::TokenStream::new(); 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(); 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() .iter()
.zip(&query_group_names_snake) .zip(&query_group_names_snake)
.zip(&query_group_storage_names) .zip(&query_group_storage_names)
.zip(&query_group_key_names) .zip(&query_group_key_names)
.zip(0_usize..)
{ {
let group_path = &query_group.group_path; let group_path = &query_group.group_path;
let group_name = query_group.name(); let group_name = query_group.name();
@ -61,6 +63,13 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
storage_fields.extend(quote! { storage_fields.extend(quote! {
#group_name_snake: #group_storage, #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 // ANCHOR:HasQueryGroup
has_group_impls.extend(quote! { has_group_impls.extend(quote! {
impl salsa::plumbing::HasQueryGroup<#group_path> for #database_name { 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 // create group storage wrapper struct
output.extend(quote! { output.extend(quote! {
#[derive(Default)]
#[doc(hidden)] #[doc(hidden)]
#visibility struct __SalsaDatabaseStorage { #visibility struct __SalsaDatabaseStorage {
#storage_fields #storage_fields
} }
impl Default for __SalsaDatabaseStorage {
fn default() -> Self {
Self {
#storage_initializers
}
}
}
}); });
// create query database_key wrapper struct // create query database_key wrapper struct

View file

@ -478,14 +478,13 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
#storage_fields #storage_fields
} }
impl<DB__> Default for #group_storage<DB__> impl<DB__> #group_storage<DB__>
where where
DB__: #trait_name + #requires, DB__: #trait_name + #requires,
DB__: salsa::plumbing::HasQueryGroup<#group_struct>, DB__: salsa::plumbing::HasQueryGroup<#group_struct>,
DB__: salsa::Database, DB__: salsa::Database,
{ {
#[inline] #trait_vis fn new(group_index: usize) -> Self {
fn default() -> Self {
#group_storage { #group_storage {
#storage_defaults #storage_defaults
} }