rust-analyzer/crates/ra-salsa/tests/interned.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

91 lines
2.4 KiB
Rust
Raw Normal View History

2024-02-07 15:29:46 +00:00
//! Test that you can implement a query using a `dyn Trait` setup.
2024-10-04 19:48:11 +00:00
use ra_salsa::InternId;
2024-02-07 15:29:46 +00:00
2024-10-04 19:48:11 +00:00
#[ra_salsa::database(InternStorage)]
2024-02-07 15:29:46 +00:00
#[derive(Default)]
struct Database {
2024-10-04 19:48:11 +00:00
storage: ra_salsa::Storage<Self>,
2024-02-07 15:29:46 +00:00
}
2024-10-04 19:48:11 +00:00
impl ra_salsa::Database for Database {}
2024-02-07 15:29:46 +00:00
2024-10-04 19:48:11 +00:00
impl ra_salsa::ParallelDatabase for Database {
fn snapshot(&self) -> ra_salsa::Snapshot<Self> {
ra_salsa::Snapshot::new(Database { storage: self.storage.snapshot() })
2024-02-07 15:29:46 +00:00
}
}
2024-10-04 19:48:11 +00:00
#[ra_salsa::query_group(InternStorage)]
2024-02-07 15:29:46 +00:00
trait Intern {
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
2024-02-07 15:29:46 +00:00
fn intern1(&self, x: String) -> InternId;
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
2024-02-07 15:29:46 +00:00
fn intern2(&self, x: String, y: String) -> InternId;
2024-10-04 19:48:11 +00:00
#[ra_salsa::interned]
2024-02-07 15:29:46 +00:00
fn intern_key(&self, x: String) -> InternKey;
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct InternKey(InternId);
2024-10-04 19:48:11 +00:00
impl ra_salsa::InternKey for InternKey {
2024-02-07 15:29:46 +00:00
fn from_intern_id(v: InternId) -> Self {
InternKey(v)
}
fn as_intern_id(&self) -> InternId {
self.0
}
}
#[test]
fn test_intern1() {
let db = Database::default();
let foo0 = db.intern1("foo".to_owned());
let bar0 = db.intern1("bar".to_owned());
let foo1 = db.intern1("foo".to_owned());
let bar1 = db.intern1("bar".to_owned());
2024-02-07 15:29:46 +00:00
assert_eq!(foo0, foo1);
assert_eq!(bar0, bar1);
assert_ne!(foo0, bar0);
assert_eq!("foo".to_owned(), db.lookup_intern1(foo0));
assert_eq!("bar".to_owned(), db.lookup_intern1(bar0));
2024-02-07 15:29:46 +00:00
}
#[test]
fn test_intern2() {
let db = Database::default();
let foo0 = db.intern2("x".to_owned(), "foo".to_owned());
let bar0 = db.intern2("x".to_owned(), "bar".to_owned());
let foo1 = db.intern2("x".to_owned(), "foo".to_owned());
let bar1 = db.intern2("x".to_owned(), "bar".to_owned());
2024-02-07 15:29:46 +00:00
assert_eq!(foo0, foo1);
assert_eq!(bar0, bar1);
assert_ne!(foo0, bar0);
assert_eq!(("x".to_owned(), "foo".to_owned()), db.lookup_intern2(foo0));
assert_eq!(("x".to_owned(), "bar".to_owned()), db.lookup_intern2(bar0));
2024-02-07 15:29:46 +00:00
}
#[test]
fn test_intern_key() {
let db = Database::default();
let foo0 = db.intern_key("foo".to_owned());
let bar0 = db.intern_key("bar".to_owned());
let foo1 = db.intern_key("foo".to_owned());
let bar1 = db.intern_key("bar".to_owned());
2024-02-07 15:29:46 +00:00
assert_eq!(foo0, foo1);
assert_eq!(bar0, bar1);
assert_ne!(foo0, bar0);
assert_eq!("foo".to_owned(), db.lookup_intern_key(foo0));
assert_eq!("bar".to_owned(), db.lookup_intern_key(bar0));
2024-02-07 15:29:46 +00:00
}