address matklad's points

This commit is contained in:
Niko Matsakis 2020-06-24 13:41:55 +00:00
parent 550c0c3db6
commit 6184b1df54

View file

@ -1,10 +1,14 @@
# Plumbing # Plumbing
**Last updated:** 2020-06-24
This chapter documents the code that salsa generates and its "inner workings". This chapter documents the code that salsa generates and its "inner workings".
We refer to this as the "plumbing". We refer to this as the "plumbing".
This page walks through the ["Hello, World!"] example and explains the code that This page walks through the ["Hello, World!"] example and explains the code that
it generates. it generates. Please take it with a grain of salt: while we make an effort to
keep this documentation up to date, this sort of thing can fall out of date
easily.
["Hello, World!"]: https://github.com/salsa-rs/salsa/blob/master/examples/hello_world/main.rs ["Hello, World!"]: https://github.com/salsa-rs/salsa/blob/master/examples/hello_world/main.rs
@ -30,10 +34,11 @@ the `salsa::query_group` macro generates a number of things:
* a group key, an enum that can identify any query within the group and store its key * a group key, an enum that can identify any query within the group and store its key
* the associated storage struct, which contains the actual hashmaps that store the data for all queries in the group * the associated storage struct, which contains the actual hashmaps that store the data for all queries in the group
Note that there are a number of structs and types (e.g., the query structs, or Note that there are a number of structs and types (e.g., the group descriptor
the group descriptor) that represent things which don't have "public" names. We and associated storage struct) that represent things which don't have "public"
currently generate mangled names but those names are not meant to be exposed to names. We currently generate mangled names with `__` afterwards, but those names
the user (ideally we'd use hygiene to enforce this). are not meant to be exposed to the user (ideally we'd use hygiene to enforce
this).
So the generated code looks something like this. We'll go into more detail on So the generated code looks something like this. We'll go into more detail on
each part in the following sections. each part in the following sections.
@ -61,8 +66,8 @@ where
} }
// Next, a series of query structs and query impls // Next, a series of query structs and query impls
struct InputString__ { } struct InputQuery { }
unsafe impl<DB> salsa::Query<DB> for InputString__ unsafe impl<DB> salsa::Query<DB> for InputQuery
where where
DB: HelloWorld, DB: HelloWorld,
DB: salsa::plumbing::HasQueryGroup<#group_struct>, DB: salsa::plumbing::HasQueryGroup<#group_struct>,
@ -70,8 +75,8 @@ where
{ {
... ...
} }
struct Length__ { } struct LengthQuery { }
unsafe impl<DB> salsa::Query<DB> for Length__ unsafe impl<DB> salsa::Query<DB> for LengthQuery
where where
DB: HelloWorld, DB: HelloWorld,
DB: salsa::plumbing::HasQueryGroup<#group_struct>, DB: salsa::plumbing::HasQueryGroup<#group_struct>,
@ -82,7 +87,7 @@ where
// For derived queries, those include implementations // For derived queries, those include implementations
// of additional traits like `QueryFunction` // of additional traits like `QueryFunction`
unsafe impl<DB> salsa::QueryFunction<DB> for Length__ unsafe impl<DB> salsa::QueryFunction<DB> for LengthQuery
where where
DB: HelloWorld, DB: HelloWorld,
DB: salsa::plumbing::HasQueryGroup<#group_struct>, DB: salsa::plumbing::HasQueryGroup<#group_struct>,
@ -163,11 +168,13 @@ check for a valid result, etc.
### For each query, a query struct ### For each query, a query struct
As we referenced in the previous section, each query in the trait gets a struct As we referenced in the previous section, each query in the trait gets a struct
with a "hidden name". This name is not used outside of the current crate/module. that represents it. This struct is named after the query, converted into snake
case and with the word `Query` appended. In typical Salsa workflows, these
structs are not meant to be named or used, but in some cases it may be required.
For e.g. the `length` query, this structs might look something like: For e.g. the `length` query, this structs might look something like:
```rust ```rust
struct HelloWorldLength__ { } struct LengthQuery { }
``` ```
The struct also implements the `plumbing::Query` trait, which defines The struct also implements the `plumbing::Query` trait, which defines
@ -175,7 +182,7 @@ a bunch of metadata about the query (and repeats, for convenience,
some of the data about the group that the query is in): some of the data about the group that the query is in):
```rust ```rust
unsafe impl<DB> salsa::Query<DB> for HelloWorldLength__ unsafe impl<DB> salsa::Query<DB> for LengthQuery
where where
DB: HelloWorld, DB: HelloWorld,
DB: salsa::plumbing::HasQueryGroup<#group_struct>, DB: salsa::plumbing::HasQueryGroup<#group_struct>,
@ -196,7 +203,7 @@ where
// query. // query.
type Storage = salsa::derived::DerivedStorage< type Storage = salsa::derived::DerivedStorage<
DB, DB,
HelloWorldLength__, LengthQuery,
salsa::plumbing::MemoizedStorage, salsa::plumbing::MemoizedStorage,
>; >;
@ -258,8 +265,8 @@ final database type:
```rust ```rust
struct HelloWorldGroupStorage__<DB> { struct HelloWorldGroupStorage__<DB> {
input: <Input as Query<DB>>::Storage, input: <InputQuery as Query<DB>>::Storage,
length: <Length as Query<DB>>::Storage, length: <LengthQuery as Query<DB>>::Storage,
} }
``` ```