salsa/tests/set_unchecked.rs
Fabian Schuiki 93c30a953d make query_group macro procedural
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.
2019-01-17 07:24:18 +01:00

91 lines
2.2 KiB
Rust

use salsa::Database;
#[salsa::query_group]
trait HelloWorldDatabase: salsa::Database {
#[salsa::input]
fn input(&self) -> String;
fn length(&self) -> usize;
fn double_length(&self) -> usize;
}
fn length(db: &impl HelloWorldDatabase) -> usize {
let l = db.input().len();
assert!(l > 0); // not meant to be invoked with no input
l
}
fn double_length(db: &impl HelloWorldDatabase) -> usize {
db.length() * 2
}
#[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 length() for LengthQuery;
fn double_length() for DoubleLengthQuery;
}
}
}
#[test]
fn normal() {
let mut db = DatabaseStruct::default();
db.query_mut(InputQuery).set((), format!("Hello, world"));
assert_eq!(db.double_length(), 24);
db.query_mut(InputQuery).set((), format!("Hello, world!"));
assert_eq!(db.double_length(), 26);
}
#[test]
#[should_panic]
fn use_without_set() {
let db = DatabaseStruct::default();
db.double_length();
}
#[test]
fn using_set_unchecked_on_input() {
let mut db = DatabaseStruct::default();
db.query_mut(InputQuery)
.set_unchecked((), format!("Hello, world"));
assert_eq!(db.double_length(), 24);
}
#[test]
fn using_set_unchecked_on_input_after() {
let mut db = DatabaseStruct::default();
db.query_mut(InputQuery).set((), format!("Hello, world"));
assert_eq!(db.double_length(), 24);
// If we use `set_unchecked`, we don't notice that `double_length`
// is out of date. Oh well, don't do that.
db.query_mut(InputQuery)
.set_unchecked((), format!("Hello, world!"));
assert_eq!(db.double_length(), 24);
}
#[test]
fn using_set_unchecked() {
let mut db = DatabaseStruct::default();
// Use `set_unchecked` to intentionally set the wrong value,
// demonstrating that the code never runs.
db.query_mut(LengthQuery).set_unchecked((), 24);
assert_eq!(db.double_length(), 48);
}