mirror of
https://github.com/salsa-rs/salsa.git
synced 2024-11-25 04:27:52 +00:00
93c30a953d
Switch to a procedural implementation of the `query_group!` macro, residing in the `components/salsa_macros` subcrate. Allow the user to override the invoked function via `salsa::invoke(...)` and the name of the generated query type via `salsa::query_type(...)`. In all tests, replace the `salsa::query_group! { ... }` invocations with the new attribute-style `#[salsa::query_group]` macro, and change them to the new naming scheme for query types (`...Query`). Update README, examples, and documentation.
68 lines
1.4 KiB
Rust
68 lines
1.4 KiB
Rust
use salsa::Database;
|
|
|
|
#[salsa::query_group]
|
|
trait HelloWorldDatabase: salsa::Database {
|
|
#[salsa::input]
|
|
fn input(&self, a: u32, b: u32) -> u32;
|
|
|
|
fn none(&self) -> u32;
|
|
|
|
fn one(&self, k: u32) -> u32;
|
|
|
|
fn two(&self, a: u32, b: u32) -> u32;
|
|
|
|
fn trailing(&self, a: u32, b: u32) -> u32;
|
|
}
|
|
|
|
fn none(_db: &impl HelloWorldDatabase) -> u32 {
|
|
22
|
|
}
|
|
|
|
fn one(_db: &impl HelloWorldDatabase, k: u32) -> u32 {
|
|
k * 2
|
|
}
|
|
|
|
fn two(_db: &impl HelloWorldDatabase, a: u32, b: u32) -> u32 {
|
|
a * b
|
|
}
|
|
|
|
fn trailing(_db: &impl HelloWorldDatabase, a: u32, b: 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 input() for InputQuery;
|
|
fn none() for NoneQuery;
|
|
fn one() for OneQuery;
|
|
fn two() for TwoQuery;
|
|
fn trailing() for TrailingQuery;
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn execute() {
|
|
let mut db = DatabaseStruct::default();
|
|
|
|
// test what happens with inputs:
|
|
db.query_mut(InputQuery).set((1, 2), 3);
|
|
assert_eq!(db.input(1, 2), 3);
|
|
|
|
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);
|
|
}
|