remove the need to list individual queries in database_storage

This commit is contained in:
Niko Matsakis 2019-01-24 05:35:57 -05:00
parent 8ad5051a74
commit c0978fede8
14 changed files with 48 additions and 160 deletions

View file

@ -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<Self> {
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<Self> {
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<Query> = 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<Self> {
let _fn_token: Token![fn ] = input.parse()?;
let query_name: Ident = input.parse()?;
let _unit: Parenthesized<Nothing> = 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 {

View file

@ -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.
///

View file

@ -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;
}
}

View file

@ -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;
}
}
```

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}