consolidate into one HasQueryGroup trait

This commit is contained in:
Niko Matsakis 2019-01-25 09:21:11 -05:00
parent 660a121a06
commit 690a118472
3 changed files with 26 additions and 40 deletions

View file

@ -39,8 +39,7 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
// For each query group `foo::MyGroup` create a link to its
// `foo::MyGroupGroupStorage`
let mut storage_fields = proc_macro2::TokenStream::new();
let mut storage_impls = proc_macro2::TokenStream::new();
let mut database_key_impls = proc_macro2::TokenStream::new();
let mut has_group_impls = proc_macro2::TokenStream::new();
for (query_group, query_group_name_snake) in query_groups.iter().zip(&query_group_names_snake) {
let group_name = query_group.name();
let group_storage = query_group.group_storage();
@ -49,22 +48,18 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
// rewrite the last identifier (`MyGroup`, above) to
// (e.g.) `MyGroupGroupStorage`.
storage_fields.extend(quote! { #query_group_name_snake: #group_storage<#database_name>, });
storage_impls.extend(quote! {
impl ::salsa::plumbing::GetQueryGroupStorage<#group_storage<#database_name>> for #database_name {
fn from(db: &Self) -> &#group_storage<#database_name> {
has_group_impls.extend(quote! {
impl ::salsa::plumbing::HasQueryGroup<#group_storage<#database_name>, #group_key>
for #database_name
{
fn group_storage(db: &Self) -> &#group_storage<#database_name> {
let runtime = ::salsa::Database::salsa_runtime(db);
&runtime.storage().#query_group_name_snake
}
}
});
// rewrite the last identifier (`MyGroup`, above) to
// (e.g.) `MyGroupGroupStorage`.
database_key_impls.extend(quote! {
impl ::salsa::plumbing::GetDatabaseKey<#group_key> for #database_name {
fn from(database_key: #group_key) -> __SalsaDatabaseKey {
fn database_key(group_key: #group_key) -> __SalsaDatabaseKey {
__SalsaDatabaseKey {
kind: __SalsaDatabaseKeyKind::#group_name(database_key),
kind: __SalsaDatabaseKeyKind::#group_name(group_key),
}
}
}
@ -122,7 +117,8 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
for query_group in query_groups {
let group_storage = query_group.group_storage();
for_each_ops.extend(quote! {
let storage: &#group_storage<#database_name> = ::salsa::plumbing::GetQueryGroupStorage::from(self);
let storage: &#group_storage<#database_name> =
::salsa::plumbing::HasQueryGroup::group_storage(self);
storage.for_each_query(self, &mut op);
});
}
@ -163,8 +159,7 @@ pub(crate) fn database(args: TokenStream, input: TokenStream) -> TokenStream {
}
});
output.extend(storage_impls);
output.extend(database_key_impls);
output.extend(has_group_impls);
if std::env::var("SALSA_DUMP").is_ok() {
println!("~~~ database_storage");

View file

@ -202,7 +202,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
// A variant for the group descriptor below
query_descriptor_maybe_change.extend(quote! {
#group_key::#fn_name(key) => {
let group_storage: &#group_storage<DB__> = ::salsa::plumbing::GetQueryGroupStorage::from(db);
let group_storage: &#group_storage<DB__> = ::salsa::plumbing::HasQueryGroup::group_storage(db);
let storage = &group_storage.#fn_name;
<_ as ::salsa::plumbing::QueryStorageOps<DB__, #qt>>::maybe_changed_since(
@ -242,8 +242,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
impl<T> #trait_name for T
where
T: #bounds,
T: ::salsa::plumbing::GetQueryGroupStorage<#group_storage<T>>,
T: ::salsa::plumbing::GetDatabaseKey<#group_key>,
T: ::salsa::plumbing::HasQueryGroup<#group_storage<T>, #group_key>,
{
#query_fn_definitions
}
@ -336,7 +335,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
) -> bool
where
DB__: #trait_name,
DB__: ::salsa::plumbing::GetQueryGroupStorage<#group_storage<DB__>>,
DB__: ::salsa::plumbing::HasQueryGroup<#group_storage<DB__>, #group_key>,
{
match self {
#query_descriptor_maybe_change
@ -362,7 +361,7 @@ pub(crate) fn query_group(args: TokenStream, input: TokenStream) -> TokenStream
impl<DB__> #group_storage<DB__>
where
DB__: #trait_name,
DB__: ::salsa::plumbing::GetQueryGroupStorage<#group_storage<DB__>>,
DB__: ::salsa::plumbing::HasQueryGroup<#group_storage<DB__>, #group_key>,
{
#trait_vis fn for_each_query(
&self,

View file

@ -86,18 +86,17 @@ impl<DB, Q> GetQueryTable<Q> for DB
where
DB: Database,
Q: Query<DB>,
DB: GetQueryGroupStorage<Q::GroupStorage>,
DB: GetDatabaseKey<Q::GroupKey>,
DB: HasQueryGroup<Q::GroupStorage, Q::GroupKey>,
{
fn get_query_table(db: &DB) -> QueryTable<'_, DB, Q> {
let group_storage: &Q::GroupStorage = GetQueryGroupStorage::from(db);
let group_storage: &Q::GroupStorage = HasQueryGroup::group_storage(db);
let query_storage = Q::group_storage(group_storage);
QueryTable::new(db, query_storage)
}
fn get_query_table_mut(db: &mut DB) -> QueryTableMut<'_, DB, Q> {
let db = &*db;
let group_storage: &Q::GroupStorage = GetQueryGroupStorage::from(db);
let group_storage: &Q::GroupStorage = HasQueryGroup::group_storage(db);
let query_storage = Q::group_storage(group_storage);
QueryTableMut::new(db, query_storage)
}
@ -107,25 +106,18 @@ where
key: <Q as Query<DB>>::Key,
) -> <DB as DatabaseStorageTypes>::DatabaseKey {
let group_key = Q::group_key(key);
<DB as GetDatabaseKey<_>>::from(group_key)
<DB as HasQueryGroup<_, _>>::database_key(group_key)
}
}
/// Access the "group storage" with type `S` from the database.
///
/// This basically moves from the full context of the database to the context
/// of one query group.
pub trait GetQueryGroupStorage<S>: Database {
fn from(db: &Self) -> &S;
}
/// Trait implemented by a database for each group that it supports.
/// `S` and `K` are the types for *group storage* and *group key*, respectively.
pub trait HasQueryGroup<S, K>: Database {
/// Access the group storage struct from the database.
fn group_storage(db: &Self) -> &S;
/// Given a group descriptor of type `D`, convert it to a full
/// database query descriptor.
///
/// This basically moves a descriptor from the context of the query
/// group into the full context of the database.
pub trait GetDatabaseKey<D>: Database {
fn from(group_key: D) -> Self::DatabaseKey;
/// "Upcast" a group key into a database key.
fn database_key(group_key: K) -> Self::DatabaseKey;
}
pub trait QueryStorageOps<DB, Q>: Default