salsa/tests/override_new_get_set.rs

81 lines
1.7 KiB
Rust
Raw Normal View History

2022-08-20 22:24:57 +00:00
//! Test that the `constructor` macro overrides
//! the `new` method's name and `get` and `set`
//! change the name of the getter and setter of the fields.
#![allow(warnings)]
use std::fmt::Display;
#[salsa::jar(db = Db)]
2024-05-24 01:16:30 +00:00
struct Jar(MyInput, MyInterned<'_>, MyTracked<'_>);
2022-08-20 22:24:57 +00:00
trait Db: salsa::DbWithJar<Jar> {}
#[salsa::input(jar = Jar, constructor = from_string)]
struct MyInput {
#[get(text)]
#[set(set_text)]
field: String,
}
impl MyInput {
pub fn new(db: &mut dyn Db, s: impl Display) -> MyInput {
MyInput::from_string(db, s.to_string())
}
pub fn field(self, db: &dyn Db) -> String {
self.text(db)
}
pub fn set_field(self, db: &mut dyn Db, id: String) {
2022-08-22 10:32:04 +00:00
self.set_text(db).to(id);
2022-08-20 22:24:57 +00:00
}
}
#[salsa::interned(constructor = from_string)]
2024-05-24 01:16:30 +00:00
struct MyInterned<'db> {
2022-08-20 22:24:57 +00:00
#[get(text)]
#[return_ref]
field: String,
}
2024-05-24 01:16:30 +00:00
impl<'db> MyInterned<'db> {
pub fn new(db: &'db dyn Db, s: impl Display) -> MyInterned<'db> {
2022-08-20 22:24:57 +00:00
MyInterned::from_string(db, s.to_string())
}
2024-05-24 01:16:30 +00:00
pub fn field(self, db: &'db dyn Db) -> &str {
2022-08-20 22:24:57 +00:00
&self.text(db)
}
}
#[salsa::tracked(constructor = from_string)]
2024-05-24 01:16:30 +00:00
struct MyTracked<'db> {
2022-08-20 22:24:57 +00:00
#[get(text)]
field: String,
}
2024-05-24 01:16:30 +00:00
impl<'db> MyTracked<'db> {
pub fn new(db: &'db dyn Db, s: impl Display) -> MyTracked<'db> {
2022-08-20 22:24:57 +00:00
MyTracked::from_string(db, s.to_string())
}
2024-05-24 01:16:30 +00:00
pub fn field(self, db: &'db dyn Db) -> String {
2022-08-20 22:24:57 +00:00
self.text(db)
}
}
#[test]
fn execute() {
#[salsa::db(Jar)]
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
impl salsa::Database for Database {}
2022-08-20 22:24:57 +00:00
impl Db for Database {}
let mut db = Database::default();
}