2018-10-05 19:29:17 +00:00
|
|
|
use salsa::Database;
|
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
#[salsa::query_group]
|
|
|
|
trait HelloWorldDatabase: salsa::Database {
|
|
|
|
#[salsa::input]
|
|
|
|
fn input(&self) -> String;
|
2018-10-05 19:29:17 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
fn length(&self) -> usize;
|
2018-10-05 19:29:17 +00:00
|
|
|
|
2019-01-12 10:11:59 +00:00
|
|
|
fn double_length(&self) -> usize;
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
fn length(db: &impl HelloWorldDatabase) -> usize {
|
|
|
|
let l = db.input().len();
|
2018-10-05 19:29:17 +00:00
|
|
|
assert!(l > 0); // not meant to be invoked with no input
|
|
|
|
l
|
|
|
|
}
|
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
fn double_length(db: &impl HelloWorldDatabase) -> usize {
|
|
|
|
db.length() * 2
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct DatabaseStruct {
|
2018-10-07 11:08:22 +00:00
|
|
|
runtime: salsa::Runtime<DatabaseStruct>,
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl salsa::Database for DatabaseStruct {
|
2018-10-07 11:08:22 +00:00
|
|
|
fn salsa_runtime(&self) -> &salsa::Runtime<DatabaseStruct> {
|
2018-10-05 19:29:17 +00:00
|
|
|
&self.runtime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
salsa::database_storage! {
|
|
|
|
struct DatabaseStorage for DatabaseStruct {
|
|
|
|
impl HelloWorldDatabase {
|
2019-01-12 10:11:59 +00:00
|
|
|
fn input() for InputQuery;
|
|
|
|
fn length() for LengthQuery;
|
|
|
|
fn double_length() for DoubleLengthQuery;
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn normal() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = DatabaseStruct::default();
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(InputQuery).set((), format!("Hello, world"));
|
2018-10-19 01:26:48 +00:00
|
|
|
assert_eq!(db.double_length(), 24);
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(InputQuery).set((), format!("Hello, world!"));
|
2018-10-19 01:26:48 +00:00
|
|
|
assert_eq!(db.double_length(), 26);
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn use_without_set() {
|
|
|
|
let db = DatabaseStruct::default();
|
2018-10-19 01:26:48 +00:00
|
|
|
db.double_length();
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn using_set_unchecked_on_input() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = DatabaseStruct::default();
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(InputQuery)
|
2018-11-01 08:30:54 +00:00
|
|
|
.set_unchecked((), format!("Hello, world"));
|
2018-10-19 01:26:48 +00:00
|
|
|
assert_eq!(db.double_length(), 24);
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn using_set_unchecked_on_input_after() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = DatabaseStruct::default();
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(InputQuery).set((), format!("Hello, world"));
|
2018-10-19 01:26:48 +00:00
|
|
|
assert_eq!(db.double_length(), 24);
|
2018-10-05 19:29:17 +00:00
|
|
|
|
|
|
|
// If we use `set_unchecked`, we don't notice that `double_length`
|
|
|
|
// is out of date. Oh well, don't do that.
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(InputQuery)
|
2018-11-01 08:30:54 +00:00
|
|
|
.set_unchecked((), format!("Hello, world!"));
|
2018-10-19 01:26:48 +00:00
|
|
|
assert_eq!(db.double_length(), 24);
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn using_set_unchecked() {
|
2018-11-01 08:30:54 +00:00
|
|
|
let mut db = DatabaseStruct::default();
|
2018-10-05 19:29:17 +00:00
|
|
|
|
|
|
|
// Use `set_unchecked` to intentionally set the wrong value,
|
|
|
|
// demonstrating that the code never runs.
|
2019-01-12 10:11:59 +00:00
|
|
|
db.query_mut(LengthQuery).set_unchecked((), 24);
|
2018-10-05 19:29:17 +00:00
|
|
|
|
2018-10-19 01:26:48 +00:00
|
|
|
assert_eq!(db.double_length(), 48);
|
2018-10-05 19:29:17 +00:00
|
|
|
}
|