update hello-world with the desired naming convention

This commit is contained in:
Niko Matsakis 2019-01-25 10:46:16 -05:00
parent 1382495d9f
commit b73cf109d7

View file

@ -5,23 +5,23 @@ use std::sync::Arc;
// A **query group** is a collection of queries (both inputs and // A **query group** is a collection of queries (both inputs and
// functions) that are defined in one particular spot. Each query // functions) that are defined in one particular spot. Each query
// group is defined by a representative struct (used internally by // group is defined by a trait decorated with the
// Salsa) as well as a representative trait. By convention, for a // `#[salsa::query_group]` attribute. The trait defines one method per
// query group `Foo`, the struct is named `Foo` and the trait is named // query, with the argments to the method being the query **keys** and
// `FooDatabase`. The name `FooDatabase` reflects the fact that the // the return value being the query's **value**.
// trait is implemented by **the database**, which stores all the data
// in the system. Each query group thus represents a subset of the
// full data.
// //
// To define a query group, you annotate a trait definition with the // Along with the trait, each query group has an associated
// `#[salsa::query_group(Foo)]` attribute macro. In addition to the // "storage struct". The name of this struct is specified in the `query_group`
// trait definition, the macro will generate a struct with the name // attribute -- for a query group `Foo`, it is conventionally `FooStorage`.
// `Foo` that you provide, as well as various other bits of glue. //
// When we define the final database (see below), we will list out the
// storage structs for each query group that it contains. The database
// will then automatically implement the traits.
// //
// Note that one query group can "include" another by listing the // Note that one query group can "include" another by listing the
// trait for that query group as a supertrait. // trait for that query group as a supertrait.
#[salsa::query_group(HelloWorld)] #[salsa::query_group(HelloWorldStorage)]
trait HelloWorldDatabase: salsa::Database { trait HelloWorld: salsa::Database {
// For each query, we give the name, some input keys (here, we // For each query, we give the name, some input keys (here, we
// have one key, `()`) and the output type `Arc<String>`. We can // have one key, `()`) and the output type `Arc<String>`. We can
// use attributes to give other configuration: // use attributes to give other configuration:
@ -48,8 +48,8 @@ trait HelloWorldDatabase: salsa::Database {
// is the "database", which is the type that contains the storage for // is the "database", which is the type that contains the storage for
// all of the queries in the system -- we never know the concrete type // all of the queries in the system -- we never know the concrete type
// here, we only know the subset of methods we care about (defined by // here, we only know the subset of methods we care about (defined by
// the `HelloWorldDatabase` trait we specified above). // the `HelloWorld` trait we specified above).
fn length(db: &impl HelloWorldDatabase, (): ()) -> usize { fn length(db: &impl HelloWorld, (): ()) -> usize {
// Read the input string: // Read the input string:
let input_string = db.input_string(()); let input_string = db.input_string(());
@ -62,16 +62,16 @@ fn length(db: &impl HelloWorldDatabase, (): ()) -> usize {
// Define the actual database struct. This struct needs to be // Define the actual database struct. This struct needs to be
// annotated with `#[salsa::database(..)]`. The list `..` will be the // annotated with `#[salsa::database(..)]`. The list `..` will be the
// paths leading to the query group structs for each query group that // paths leading to the storage structs for each query group that this
// this database supports. This attribute macro will generate the // database supports. This attribute macro will generate the necessary
// necessary impls so that the database implements the corresponding // impls so that the database implements the corresponding traits as
// traits as well (so, here, `DatabaseStruct` will implement the // well (so, here, `DatabaseStruct` will implement the `HelloWorld`
// `HelloWorldDatabase` trait). // trait).
// //
// The database struct can contain basically anything you need, but it // The database struct can contain basically anything you need, but it
// must have a `runtime` field as shown, and you must implement the // must have a `runtime` field as shown, and you must implement the
// `salsa::Database` trait (as shown below). // `salsa::Database` trait (as shown below).
#[salsa::database(HelloWorld)] #[salsa::database(HelloWorldStorage)]
#[derive(Default)] #[derive(Default)]
struct DatabaseStruct { struct DatabaseStruct {
runtime: salsa::Runtime<DatabaseStruct>, runtime: salsa::Runtime<DatabaseStruct>,