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.
77 lines
1.7 KiB
Rust
77 lines
1.7 KiB
Rust
use crate::implementation::{TestContext, TestContextImpl};
|
|
use salsa::Database;
|
|
|
|
#[salsa::query_group]
|
|
pub(crate) trait MemoizedInputsContext: TestContext {
|
|
fn max(&self) -> usize;
|
|
#[salsa::input]
|
|
fn input1(&self) -> usize;
|
|
#[salsa::input]
|
|
fn input2(&self) -> usize;
|
|
}
|
|
|
|
fn max(db: &impl MemoizedInputsContext) -> usize {
|
|
db.log().add("Max invoked");
|
|
std::cmp::max(db.input1(), db.input2())
|
|
}
|
|
|
|
#[test]
|
|
fn revalidate() {
|
|
let db = &mut TestContextImpl::default();
|
|
|
|
db.query_mut(Input1Query).set((), 0);
|
|
db.query_mut(Input2Query).set((), 0);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 0);
|
|
db.assert_log(&["Max invoked"]);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 0);
|
|
db.assert_log(&[]);
|
|
|
|
db.query_mut(Input1Query).set((), 44);
|
|
db.assert_log(&[]);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 44);
|
|
db.assert_log(&["Max invoked"]);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 44);
|
|
db.assert_log(&[]);
|
|
|
|
db.query_mut(Input1Query).set((), 44);
|
|
db.assert_log(&[]);
|
|
db.query_mut(Input2Query).set((), 66);
|
|
db.assert_log(&[]);
|
|
db.query_mut(Input1Query).set((), 64);
|
|
db.assert_log(&[]);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 66);
|
|
db.assert_log(&["Max invoked"]);
|
|
|
|
let v = db.max();
|
|
assert_eq!(v, 66);
|
|
db.assert_log(&[]);
|
|
}
|
|
|
|
/// Test that invoking `set` on an input with the same value still
|
|
/// triggers a new revision.
|
|
#[test]
|
|
fn set_after_no_change() {
|
|
let db = &mut TestContextImpl::default();
|
|
|
|
db.query_mut(Input2Query).set((), 0);
|
|
|
|
db.query_mut(Input1Query).set((), 44);
|
|
let v = db.max();
|
|
assert_eq!(v, 44);
|
|
db.assert_log(&["Max invoked"]);
|
|
|
|
db.query_mut(Input1Query).set((), 44);
|
|
let v = db.max();
|
|
assert_eq!(v, 44);
|
|
db.assert_log(&["Max invoked"]);
|
|
}
|