mirror of
https://github.com/salsa-rs/salsa.git
synced 2024-11-25 04:27:52 +00:00
support variadic queries in the trait definition
This commit is contained in:
parent
089f489df6
commit
ed2cf2333f
2 changed files with 71 additions and 4 deletions
|
@ -190,7 +190,7 @@ macro_rules! query_group {
|
|||
tokens[{
|
||||
$(
|
||||
$(#[$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;
|
||||
$(storage $storage:ident;)* // 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)* {
|
||||
$(
|
||||
$(#[$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)
|
||||
.get(key)
|
||||
.get(($($key_name),*))
|
||||
}
|
||||
)*
|
||||
}
|
||||
|
@ -216,7 +216,7 @@ macro_rules! query_group {
|
|||
where
|
||||
DB: $query_trait,
|
||||
{
|
||||
type Key = $key_ty;
|
||||
type Key = ($($key_ty),*);
|
||||
type Value = $value_ty;
|
||||
type Storage = $crate::query_group! { @storage_ty[DB, Self, $($storage)*] };
|
||||
}
|
||||
|
|
67
tests/variadic.rs
Normal file
67
tests/variadic.rs
Normal 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);
|
||||
}
|
Loading…
Reference in a new issue