From 868904874af826a9eb353815809a6fe66675e56a Mon Sep 17 00:00:00 2001 From: Cormac Relf Date: Sat, 26 Jan 2019 02:45:07 +1100 Subject: [PATCH] manually implement Default for *GroupStorage All the example code uses Default to create the db structs, but it turns out the *GroupStorage's `#[derive(Default)]` adds a trait bound of (e.g.) `DB__ + Default + HelloWorldDatabase` even though it never actually needs to call HelloWorldDatabase::default(). So if you didn't implement Default, then you couldn't be a salsa database struct. --- components/salsa-macros/src/query_group.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/components/salsa-macros/src/query_group.rs b/components/salsa-macros/src/query_group.rs index b1471516..9759f7a0 100644 --- a/components/salsa-macros/src/query_group.rs +++ b/components/salsa-macros/src/query_group.rs @@ -135,6 +135,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream let mut query_descriptor_variants = proc_macro2::TokenStream::new(); let mut query_descriptor_maybe_change = proc_macro2::TokenStream::new(); let mut storage_fields = proc_macro2::TokenStream::new(); + let mut storage_defaults = proc_macro2::TokenStream::new(); for query in &queries { let key_names: &Vec<_> = &(0..query.keys.len()) .map(|i| Ident::new(&format!("key{}", i), Span::call_site())) @@ -221,6 +222,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream storage_fields.extend(quote! { pub #fn_name: <#qt as ::salsa::Query>::Storage, }); + storage_defaults.extend(quote! { #fn_name: Default::default(), }); } // Emit the trait itself. @@ -353,12 +355,22 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream } // Emit query group storage struct + // It would derive Default, but then all database structs would have to implement Default + // as the derived version includes an unused `+ Default` constraint. output.extend(quote! { - #[derive(Default)] #trait_vis struct #group_storage { #storage_fields } + impl Default for #group_storage { + #[inline] + fn default() -> Self { + #group_storage { + #storage_defaults + } + } + } + impl #group_storage where DB__: #trait_name,