support variadic queries in the trait definition

This commit is contained in:
Niko Matsakis 2018-10-18 19:24:38 -04:00
parent 089f489df6
commit ed2cf2333f
2 changed files with 71 additions and 4 deletions

View file

@ -190,7 +190,7 @@ macro_rules! query_group {
tokens[{ tokens[{
$( $(
$(#[$method_attr:meta])* $(#[$method_attr:meta])*
fn $method_name:ident($key_name:ident: $key_ty:ty) -> $value_ty:ty { fn $method_name:ident($($key_name:ident: $key_ty:ty),* $(,)*) -> $value_ty:ty {
type $QueryType:ident; type $QueryType:ident;
$(storage $storage:ident;)* // FIXME(rust-lang/rust#48075) should be `?` $(storage $storage:ident;)* // FIXME(rust-lang/rust#48075) should be `?`
$(use fn $fn_path:path;)* // FIXME(rust-lang/rust#48075) should be `?` $(use fn $fn_path:path;)* // FIXME(rust-lang/rust#48075) should be `?`
@ -201,9 +201,9 @@ macro_rules! query_group {
$($trait_attr)* $v trait $query_trait: $($crate::plumbing::GetQueryTable<$QueryType> +)* $($header)* { $($trait_attr)* $v trait $query_trait: $($crate::plumbing::GetQueryTable<$QueryType> +)* $($header)* {
$( $(
$(#[$method_attr])* $(#[$method_attr])*
fn $method_name(&self, key: $key_ty) -> $value_ty { fn $method_name(&self, $($key_name: $key_ty),*) -> $value_ty {
<Self as $crate::plumbing::GetQueryTable<$QueryType>>::get_query_table(self) <Self as $crate::plumbing::GetQueryTable<$QueryType>>::get_query_table(self)
.get(key) .get(($($key_name),*))
} }
)* )*
} }
@ -216,7 +216,7 @@ macro_rules! query_group {
where where
DB: $query_trait, DB: $query_trait,
{ {
type Key = $key_ty; type Key = ($($key_ty),*);
type Value = $value_ty; type Value = $value_ty;
type Storage = $crate::query_group! { @storage_ty[DB, Self, $($storage)*] }; type Storage = $crate::query_group! { @storage_ty[DB, Self, $($storage)*] };
} }

67
tests/variadic.rs Normal file
View file

@ -0,0 +1,67 @@
salsa::query_group! {
trait HelloWorldDatabase: salsa::Database {
fn none() -> u32 {
type None;
}
fn one(k: u32) -> u32 {
type One;
}
fn two(a: u32, b: u32) -> u32 {
type Two;
}
fn trailing(a: u32, b: u32,) -> u32 {
type Trailing;
}
}
}
fn none(_db: &impl HelloWorldDatabase, (): ()) -> u32 {
22
}
fn one(_db: &impl HelloWorldDatabase, k: u32) -> u32 {
k * 2
}
fn two(_db: &impl HelloWorldDatabase, (a, b): (u32, u32)) -> u32 {
a * b
}
fn trailing(_db: &impl HelloWorldDatabase, (a, b): (u32, u32)) -> u32 {
a - b
}
#[derive(Default)]
struct DatabaseStruct {
runtime: salsa::Runtime<DatabaseStruct>,
}
impl salsa::Database for DatabaseStruct {
fn salsa_runtime(&self) -> &salsa::Runtime<DatabaseStruct> {
&self.runtime
}
}
salsa::database_storage! {
struct DatabaseStorage for DatabaseStruct {
impl HelloWorldDatabase {
fn none() for None;
fn one() for One;
fn two() for Two;
fn trailing() for Trailing;
}
}
}
#[test]
fn execute() {
let db = DatabaseStruct::default();
assert_eq!(db.none(), 22);
assert_eq!(db.one(11), 22);
assert_eq!(db.two(11, 2), 22);
assert_eq!(db.trailing(24, 2), 22);
}