mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-23 05:07:27 +00:00
Add test override_new_get_set
This commit is contained in:
parent
958b063a60
commit
6b1d24098b
1 changed files with 84 additions and 0 deletions
84
salsa-2022-tests/tests/override_new_get_set.rs
Normal file
84
salsa-2022-tests/tests/override_new_get_set.rs
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
//! 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)]
|
||||||
|
struct Jar(MyInput, MyInterned, MyTracked);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
self.set_text(db, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::interned(constructor = from_string)]
|
||||||
|
struct MyInterned {
|
||||||
|
#[get(text)]
|
||||||
|
#[return_ref]
|
||||||
|
field: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyInterned {
|
||||||
|
pub fn new(db: &dyn Db, s: impl Display) -> MyInterned {
|
||||||
|
MyInterned::from_string(db, s.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn field(self, db: &dyn Db) -> &str {
|
||||||
|
&self.text(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::tracked(constructor = from_string)]
|
||||||
|
struct MyTracked {
|
||||||
|
#[get(text)]
|
||||||
|
field: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyTracked {
|
||||||
|
pub fn new(db: &dyn Db, s: impl Display) -> MyTracked {
|
||||||
|
MyTracked::from_string(db, s.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn field(self, db: &dyn Db) -> String {
|
||||||
|
self.text(db)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn execute() {
|
||||||
|
#[salsa::db(Jar)]
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Database {
|
||||||
|
storage: salsa::Storage<Self>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl salsa::Database for Database {
|
||||||
|
fn salsa_runtime(&self) -> &salsa::Runtime {
|
||||||
|
self.storage.runtime()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Db for Database {}
|
||||||
|
|
||||||
|
let mut db = Database::default();
|
||||||
|
}
|
Loading…
Reference in a new issue