diff --git a/src/lib.rs b/src/lib.rs index 682bda7..ccdc94c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { >::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)*] }; } diff --git a/tests/variadic.rs b/tests/variadic.rs new file mode 100644 index 0000000..ec246b7 --- /dev/null +++ b/tests/variadic.rs @@ -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, +} + +impl salsa::Database for DatabaseStruct { + fn salsa_runtime(&self) -> &salsa::Runtime { + &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); +}