2019-02-03 15:47:18 +00:00
|
|
|
//! Test that you can implement a query using a `dyn Trait` setup.
|
|
|
|
|
2019-04-03 14:00:59 +00:00
|
|
|
use salsa::InternId;
|
2019-03-30 09:43:16 +00:00
|
|
|
|
2019-02-03 15:47:18 +00:00
|
|
|
#[salsa::database(InternStorage)]
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Database {
|
|
|
|
runtime: salsa::Runtime<Database>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl salsa::Database for Database {
|
|
|
|
fn salsa_runtime(&self) -> &salsa::Runtime<Database> {
|
|
|
|
&self.runtime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 11:33:59 +00:00
|
|
|
impl salsa::ParallelDatabase for Database {
|
|
|
|
fn snapshot(&self) -> salsa::Snapshot<Self> {
|
|
|
|
salsa::Snapshot::new(Database {
|
|
|
|
runtime: self.runtime.snapshot(self),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-03 15:47:18 +00:00
|
|
|
#[salsa::query_group(InternStorage)]
|
|
|
|
trait Intern {
|
|
|
|
#[salsa::interned]
|
2019-04-03 14:00:59 +00:00
|
|
|
fn intern1(&self, x: String) -> InternId;
|
2019-02-03 15:47:18 +00:00
|
|
|
|
|
|
|
#[salsa::interned]
|
2019-04-03 14:00:59 +00:00
|
|
|
fn intern2(&self, x: String, y: String) -> InternId;
|
2019-02-03 15:47:18 +00:00
|
|
|
|
|
|
|
#[salsa::interned]
|
|
|
|
fn intern_key(&self, x: String) -> InternKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
2019-04-03 14:00:59 +00:00
|
|
|
pub struct InternKey(InternId);
|
2019-02-03 15:47:18 +00:00
|
|
|
|
2019-02-04 20:01:23 +00:00
|
|
|
impl salsa::InternKey for InternKey {
|
2019-04-03 14:00:59 +00:00
|
|
|
fn from_intern_id(v: InternId) -> Self {
|
2019-02-03 15:47:18 +00:00
|
|
|
InternKey(v)
|
|
|
|
}
|
|
|
|
|
2019-04-03 14:00:59 +00:00
|
|
|
fn as_intern_id(&self) -> InternId {
|
2019-02-03 15:47:18 +00:00
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intern1() {
|
2019-02-04 21:21:24 +00:00
|
|
|
let db = Database::default();
|
2019-02-03 15:47:18 +00:00
|
|
|
let foo0 = db.intern1(format!("foo"));
|
|
|
|
let bar0 = db.intern1(format!("bar"));
|
|
|
|
let foo1 = db.intern1(format!("foo"));
|
|
|
|
let bar1 = db.intern1(format!("bar"));
|
|
|
|
|
|
|
|
assert_eq!(foo0, foo1);
|
|
|
|
assert_eq!(bar0, bar1);
|
|
|
|
assert_ne!(foo0, bar0);
|
|
|
|
|
|
|
|
assert_eq!(format!("foo"), db.lookup_intern1(foo0));
|
|
|
|
assert_eq!(format!("bar"), db.lookup_intern1(bar0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intern2() {
|
2019-02-04 21:21:24 +00:00
|
|
|
let db = Database::default();
|
2019-02-03 15:47:18 +00:00
|
|
|
let foo0 = db.intern2(format!("x"), format!("foo"));
|
|
|
|
let bar0 = db.intern2(format!("x"), format!("bar"));
|
|
|
|
let foo1 = db.intern2(format!("x"), format!("foo"));
|
|
|
|
let bar1 = db.intern2(format!("x"), format!("bar"));
|
|
|
|
|
|
|
|
assert_eq!(foo0, foo1);
|
|
|
|
assert_eq!(bar0, bar1);
|
|
|
|
assert_ne!(foo0, bar0);
|
|
|
|
|
|
|
|
assert_eq!((format!("x"), format!("foo")), db.lookup_intern2(foo0));
|
|
|
|
assert_eq!((format!("x"), format!("bar")), db.lookup_intern2(bar0));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intern_key() {
|
2019-02-04 21:21:24 +00:00
|
|
|
let db = Database::default();
|
2019-02-03 15:47:18 +00:00
|
|
|
let foo0 = db.intern_key(format!("foo"));
|
|
|
|
let bar0 = db.intern_key(format!("bar"));
|
|
|
|
let foo1 = db.intern_key(format!("foo"));
|
|
|
|
let bar1 = db.intern_key(format!("bar"));
|
|
|
|
|
|
|
|
assert_eq!(foo0, foo1);
|
|
|
|
assert_eq!(bar0, bar1);
|
|
|
|
assert_ne!(foo0, bar0);
|
|
|
|
|
|
|
|
assert_eq!(format!("foo"), db.lookup_intern_key(foo0));
|
|
|
|
assert_eq!(format!("bar"), db.lookup_intern_key(bar0));
|
|
|
|
}
|