salsa/tests/tracked_method_on_tracked_struct.rs
Niko Matsakis daaa78056a switch to new database design
Under this design, *all* databases are a
`DatabaseImpl<U>`, where the `U` implements
`UserData` (you can use `()` if there is none).

Code would default to `&dyn salsa::Database` but
if you want to give access to the userdata, you
can define a custom database trait
`MyDatabase: salsa::Databse` so long as you

* annotate `MyDatabase` trait definition of
  impls of `MyDatabase` with `#[salsa::db]`
* implement `MyDatabase` for `DatabaseImpl<U>`
  where `U` is your userdata (this could be a
  blanket impl, if you don't know the precise
  userdata type).

The `tests/common/mod.rs` shows the pattern.
2024-07-28 12:47:50 +00:00

66 lines
1.5 KiB
Rust

use salsa::Database;
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct Item {}
#[salsa::input]
pub struct Input {
name: String,
}
#[salsa::tracked]
impl Input {
#[salsa::tracked]
pub fn source_tree(self, db: &dyn Database) -> SourceTree<'_> {
SourceTree::new(db, self.name(db).clone())
}
}
#[salsa::tracked]
pub struct SourceTree<'db> {
name: String,
}
#[salsa::tracked]
impl<'db1> SourceTree<'db1> {
#[salsa::tracked(return_ref)]
pub fn inherent_item_name(self, db: &'db1 dyn Database) -> String {
self.name(db)
}
}
trait ItemName<'db1> {
fn trait_item_name(self, db: &'db1 dyn Database) -> &'db1 String;
}
#[salsa::tracked]
impl<'db1> ItemName<'db1> for SourceTree<'db1> {
#[salsa::tracked(return_ref)]
fn trait_item_name(self, db: &'db1 dyn Database) -> String {
self.name(db)
}
}
#[test]
fn test_inherent() {
salsa::DatabaseImpl::new().attach(|db| {
let input = Input::new(db, "foo".to_string());
let source_tree = input.source_tree(db);
expect_test::expect![[r#"
"foo"
"#]]
.assert_debug_eq(source_tree.inherent_item_name(db));
})
}
#[test]
fn test_trait() {
salsa::DatabaseImpl::new().attach(|db| {
let input = Input::new(db, "foo".to_string());
let source_tree = input.source_tree(db);
expect_test::expect![[r#"
"foo"
"#]]
.assert_debug_eq(source_tree.trait_item_name(db));
})
}