From c0978fede8c0993018e309b9e1a0085df49d5039 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Thu, 24 Jan 2019 05:35:57 -0500 Subject: [PATCH] remove the need to list individual queries in `database_storage` --- .../salsa-macros/src/database_storage.rs | 46 ++----------------- components/salsa-macros/src/lib.rs | 23 +++++----- examples/compiler/implementation.rs | 8 +--- examples/hello_world/README.md | 20 +++----- examples/hello_world/main.rs | 11 ++--- tests/cycles.rs | 9 +--- tests/gc/db.rs | 12 +---- tests/incremental/implementation.rs | 27 ++--------- tests/panic_safely.rs | 7 +-- tests/parallel/setup.rs | 12 +---- tests/parallel/stress.rs | 8 +--- tests/set_unchecked.rs | 8 +--- tests/storage_varieties/implementation.rs | 7 +-- tests/variadic.rs | 10 +--- 14 files changed, 48 insertions(+), 160 deletions(-) diff --git a/components/salsa-macros/src/database_storage.rs b/components/salsa-macros/src/database_storage.rs index 30c605d8..a0cfa7d3 100644 --- a/components/salsa-macros/src/database_storage.rs +++ b/components/salsa-macros/src/database_storage.rs @@ -1,4 +1,3 @@ -use crate::parenthesized::Parenthesized; use heck::SnakeCase; use proc_macro::TokenStream; use proc_macro2::Span; @@ -11,12 +10,7 @@ use syn::{Attribute, Ident, Path, Token, Visibility}; /// /// ```ignore /// salsa::database_storage! { -/// struct DatabaseStorage for DatabaseStruct { -/// impl HelloWorldDatabase { -/// fn input_string() for InputString; -/// fn length() for LengthQuery; -/// } -/// } +/// $vis DatabaseStruct { impl HelloWorldDatabase; } /// } /// ``` /// @@ -246,19 +240,10 @@ impl QueryGroup { } } -#[allow(dead_code)] -struct Query { - query_name: Ident, - query_type: Path, -} - impl Parse for DatabaseStorage { fn parse(input: ParseStream) -> syn::Result { let attributes = input.call(Attribute::parse_outer)?; let visibility = input.parse()?; - let _struct_token: Token![struct ] = input.parse()?; - let _storage_struct_name: Ident = input.parse()?; - let _for_token: Token![for ] = input.parse()?; let database_name: Path = input.parse()?; let content; syn::braced!(content in input); @@ -274,39 +259,16 @@ impl Parse for DatabaseStorage { impl Parse for QueryGroup { /// ```ignore - /// impl HelloWorldDatabase { - /// fn input_string() for InputString; - /// fn length() for LengthQuery; - /// } + /// impl HelloWorldDatabase; /// ``` fn parse(input: ParseStream) -> syn::Result { - let _fn_token: Token![impl ] = input.parse()?; + let _: Token![impl ] = input.parse()?; let query_group: Path = input.parse()?; - let content; - syn::braced!(content in input); - let _queries: Vec = parse_while(Token![fn ], &content)?; + let _: Token![;] = input.parse()?; Ok(QueryGroup { query_group }) } } -impl Parse for Query { - /// ```ignore - /// fn input_string() for InputString; - /// ``` - fn parse(input: ParseStream) -> syn::Result { - let _fn_token: Token![fn ] = input.parse()?; - let query_name: Ident = input.parse()?; - let _unit: Parenthesized = input.parse()?; - let _for_token: Token![for ] = input.parse()?; - let query_type: Path = input.parse()?; - let _for_token: Token![;] = input.parse()?; - Ok(Query { - query_name, - query_type, - }) - } -} - struct Nothing; impl Parse for Nothing { diff --git a/components/salsa-macros/src/lib.rs b/components/salsa-macros/src/lib.rs index 42d6a736..48dd1849 100644 --- a/components/salsa-macros/src/lib.rs +++ b/components/salsa-macros/src/lib.rs @@ -126,26 +126,25 @@ pub fn query_group(args: TokenStream, input: TokenStream) -> TokenStream { query_group::query_group(args, input) } -/// This macro generates the "query storage" that goes into your database. -/// It requires you to list all of the query groups that you need as well -/// as the queries within those groups. The format looks like so: +/// This macro generates the "query storage" that goes into your +/// database. It requires you to list all of the query groups that +/// you need. The format looks like so: /// /// ```rust,ignore /// salsa::database_storage! { -/// struct MyDatabaseStorage for MyDatabase { -/// impl MyQueryGroup { -/// fn my_query1() for MyQuery1; -/// fn my_query2() for MyQuery2; -/// } +/// $v MyDatabase { +/// impl MyQueryGroup; /// // ... other query groups go here ... /// } /// } /// ``` /// -/// Here, `MyDatabase` should be the name of your database type. The -/// macro will then generate a struct named `MyDatabaseStorage` that -/// is used by the [`salsa::Runtime`]. `MyQueryGroup` should be the -/// name of your query group. +/// Here, `MyDatabase` should be the name of your database type, and +/// `$v` should be the "visibility" of that struct (e.g., `pub`, +/// `pub(crate)`, or just nothing). The macro will then generate a +/// struct named `MyDatabaseStorage` that is used by the +/// [`salsa::Runtime`]. `MyQueryGroup` should be the name of your +/// query group. /// /// See [the `hello_world` example][hw] for more details. /// diff --git a/examples/compiler/implementation.rs b/examples/compiler/implementation.rs index 2e48fc23..3c41b5ba 100644 --- a/examples/compiler/implementation.rs +++ b/examples/compiler/implementation.rs @@ -37,12 +37,8 @@ impl salsa::Database for DatabaseImpl { /// storage and also generate impls for those traits, so that you /// `DatabaseImpl` type implements them. salsa::database_storage! { - pub struct DatabaseImplStorage for DatabaseImpl { - impl class_table::ClassTableDatabase { - fn all_classes() for class_table::AllClassesQuery; - fn all_fields() for class_table::AllFieldsQuery; - fn fields() for class_table::FieldsQuery; - } + pub DatabaseImpl { + impl class_table::ClassTableDatabase; } } diff --git a/examples/hello_world/README.md b/examples/hello_world/README.md index 1be97636..e7f803cc 100644 --- a/examples/hello_world/README.md +++ b/examples/hello_world/README.md @@ -133,23 +133,15 @@ impl salsa::Database for DatabaseStruct { } ``` -Next, you must use the `database_storage!` to define the "storage -struct" for your type. This storage struct contains all the hashmaps -and other things that salsa uses to store the values for your -queries. You won't need to interact with it directly. To use the -macro, you basically list out all the traits and each of the queries -within those traits: +Next, you must use the `database_storage!` to specify the set of query +groups that your database stores. This macro generates the internal +storage struct used to store your data. To use the macro, you +basically list out all the traits: ```rust salsa::database_storage! { - struct DatabaseStorage for DatabaseStruct { - // ^^^^^^^^^^^^^^^ -------------- - // name of the type the name of your context type - // we will make - impl HelloWorldDatabase { - fn input_string() for InputString; - fn length() for Length; - } + DatabaseStruct { // <-- name of your context type + impl HelloWorldDatabase; } } ``` diff --git a/examples/hello_world/main.rs b/examples/hello_world/main.rs index 979d8bdb..83b16255 100644 --- a/examples/hello_world/main.rs +++ b/examples/hello_world/main.rs @@ -68,16 +68,13 @@ impl salsa::Database for DatabaseStruct { } } -// Define the full set of queries that your context needs. This would +// Define the full set of query groups that your context needs. This would // in general combine (and implement) all the database traits in // your application into one place, allocating storage for all of -// them. +// them. But here we have only one. salsa::database_storage! { - struct DatabaseStorage for DatabaseStruct { - impl HelloWorldDatabase { - fn input_string() for InputString; - fn length() for LengthQuery; - } + DatabaseStruct { + impl HelloWorldDatabase; } } diff --git a/tests/cycles.rs b/tests/cycles.rs index b63694a2..de775d85 100644 --- a/tests/cycles.rs +++ b/tests/cycles.rs @@ -10,13 +10,8 @@ impl salsa::Database for DatabaseImpl { } salsa::database_storage! { - struct DatabaseImplStorage for DatabaseImpl { - impl Database { - fn memoized_a() for MemoizedAQuery; - fn memoized_b() for MemoizedBQuery; - fn volatile_a() for VolatileAQuery; - fn volatile_b() for VolatileBQuery; - } + DatabaseImpl { + impl Database; } } diff --git a/tests/gc/db.rs b/tests/gc/db.rs index d5528066..9fdec151 100644 --- a/tests/gc/db.rs +++ b/tests/gc/db.rs @@ -14,16 +14,8 @@ impl salsa::Database for DatabaseImpl { } salsa::database_storage! { - pub(crate) struct DatabaseImplStorage for DatabaseImpl { - impl group::GcDatabase { - fn min() for group::MinQuery; - fn max() for group::MaxQuery; - fn use_triangular() for group::UseTriangularQuery; - fn fibonacci() for group::FibonacciQuery; - fn triangular() for group::TriangularQuery; - fn compute() for group::ComputeQuery; - fn compute_all() for group::ComputeAllQuery; - } + pub(crate) DatabaseImpl { + impl group::GcDatabase; } } diff --git a/tests/incremental/implementation.rs b/tests/incremental/implementation.rs index 9c2bbb80..6cbd27c6 100644 --- a/tests/incremental/implementation.rs +++ b/tests/incremental/implementation.rs @@ -39,31 +39,14 @@ impl TestContextImpl { } salsa::database_storage! { - pub(crate) struct TestContextImplStorage for TestContextImpl { - impl constants::ConstantsDatabase { - fn constants_input() for constants::ConstantsInputQuery; - fn constants_add() for constants::ConstantsAddQuery; - } + pub(crate) TestContextImpl { + impl constants::ConstantsDatabase; - impl memoized_dep_inputs::MemoizedDepInputsContext { - fn dep_memoized2() for memoized_dep_inputs::DepMemoized2Query; - fn dep_memoized1() for memoized_dep_inputs::DepMemoized1Query; - fn dep_derived1() for memoized_dep_inputs::DepDerived1Query; - fn dep_input1() for memoized_dep_inputs::DepInput1Query; - fn dep_input2() for memoized_dep_inputs::DepInput2Query; - } + impl memoized_dep_inputs::MemoizedDepInputsContext; - impl memoized_inputs::MemoizedInputsContext { - fn max() for memoized_inputs::MaxQuery; - fn input1() for memoized_inputs::Input1Query; - fn input2() for memoized_inputs::Input2Query; - } + impl memoized_inputs::MemoizedInputsContext; - impl memoized_volatile::MemoizedVolatileContext { - fn memoized2() for memoized_volatile::Memoized2Query; - fn memoized1() for memoized_volatile::Memoized1Query; - fn volatile() for memoized_volatile::VolatileQuery; - } + impl memoized_volatile::MemoizedVolatileContext; } } diff --git a/tests/panic_safely.rs b/tests/panic_safely.rs index 8f256674..0c36a6f7 100644 --- a/tests/panic_safely.rs +++ b/tests/panic_safely.rs @@ -33,11 +33,8 @@ impl salsa::ParallelDatabase for DatabaseStruct { } salsa::database_storage! { - struct DatabaseStorage for DatabaseStruct { - impl PanicSafelyDatabase { - fn one() for OneQuery; - fn panic_safely() for PanicSafelyQuery; - } + DatabaseStruct { + impl PanicSafelyDatabase; } } diff --git a/tests/parallel/setup.rs b/tests/parallel/setup.rs index 8685948a..9428c70f 100644 --- a/tests/parallel/setup.rs +++ b/tests/parallel/setup.rs @@ -235,15 +235,7 @@ impl Knobs for ParDatabaseImpl { } salsa::database_storage! { - pub(crate) struct DatabaseImplStorage for ParDatabaseImpl { - impl ParDatabase { - fn input() for InputQuery; - fn sum() for SumQuery; - fn sum2() for Sum2Query; - fn sum2_drop_sum() for Sum2DropSumQuery; - fn sum3() for Sum3Query; - fn sum3_drop_sum() for Sum3DropSumQuery; - fn snapshot_me() for SnapshotMeQuery; - } + pub(crate) ParDatabaseImpl { + impl ParDatabase; } } diff --git a/tests/parallel/stress.rs b/tests/parallel/stress.rs index aa93e431..5be83785 100644 --- a/tests/parallel/stress.rs +++ b/tests/parallel/stress.rs @@ -53,12 +53,8 @@ impl salsa::ParallelDatabase for StressDatabaseImpl { } salsa::database_storage! { - pub struct DatabaseImplStorage for StressDatabaseImpl { - impl StressDatabase { - fn a() for AQuery; - fn b() for BQuery; - fn c() for CQuery; - } + pub StressDatabaseImpl { + impl StressDatabase; } } diff --git a/tests/set_unchecked.rs b/tests/set_unchecked.rs index 4147f29a..acaa5204 100644 --- a/tests/set_unchecked.rs +++ b/tests/set_unchecked.rs @@ -32,12 +32,8 @@ impl salsa::Database for DatabaseStruct { } salsa::database_storage! { - struct DatabaseStorage for DatabaseStruct { - impl HelloWorldDatabase { - fn input() for InputQuery; - fn length() for LengthQuery; - fn double_length() for DoubleLengthQuery; - } + DatabaseStruct { + impl HelloWorldDatabase; } } diff --git a/tests/storage_varieties/implementation.rs b/tests/storage_varieties/implementation.rs index cf74bc36..ac677824 100644 --- a/tests/storage_varieties/implementation.rs +++ b/tests/storage_varieties/implementation.rs @@ -8,11 +8,8 @@ pub(crate) struct DatabaseImpl { } salsa::database_storage! { - pub(crate) struct DatabaseImplStorage for DatabaseImpl { - impl queries::Database { - fn memoized() for queries::MemoizedQuery; - fn volatile() for queries::VolatileQuery; - } + pub(crate) DatabaseImpl { + impl queries::Database; } } diff --git a/tests/variadic.rs b/tests/variadic.rs index 8ac3e042..688fd274 100644 --- a/tests/variadic.rs +++ b/tests/variadic.rs @@ -42,14 +42,8 @@ impl salsa::Database for DatabaseStruct { } salsa::database_storage! { - struct DatabaseStorage for DatabaseStruct { - impl HelloWorldDatabase { - fn input() for InputQuery; - fn none() for NoneQuery; - fn one() for OneQuery; - fn two() for TwoQuery; - fn trailing() for TrailingQuery; - } + DatabaseStruct { + impl HelloWorldDatabase; } }