salsa/tests/override_new_get_set.rs

83 lines
1.6 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;
2024-07-17 10:14:43 +00:00
use salsa::Setter;
2024-07-17 10:08:34 +00:00
#[salsa::db]
trait Db: salsa::Database {}
2022-08-20 22:24:57 +00:00
2024-07-17 10:08:34 +00:00
#[salsa::input(constructor = from_string)]
2022-08-20 22:24:57 +00:00
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() {
2024-07-17 10:08:34 +00:00
#[salsa::db]
2022-08-20 22:24:57 +00:00
#[derive(Default)]
struct Database {
storage: salsa::Storage<Self>,
}
2024-07-17 10:08:34 +00:00
#[salsa::db]
impl salsa::Database for Database {}
2022-08-20 22:24:57 +00:00
2024-07-17 10:08:34 +00:00
#[salsa::db]
2022-08-20 22:24:57 +00:00
impl Db for Database {}
let mut db = Database::default();
}