You can't. This is a self-referential struct, which Rust can't really support. Such a struct would have its pointer-to-itself invalidated when moved. What are you trying to do? Usually, there are better ways to solve problems in Rust than self-referential types.
This wouldn't work in general. For example, trying to return Database from the function (or, more generally, move it out of its creation point) would fail - playground:
// struct Database and macro test - as above
fn get_db<'a>() -> Database<'a> {
let mut db: Database = Database {
d1: "a".to_string(),
d2: &"".to_string(),
};
test!(db);
db
}
fn main() {
let db = get_db();
println!("{:?}", db);
}
Errors:
error[E0505]: cannot move out of `db` because it is borrowed
--> src/main.rs:19:5
|
11 | $x.d2 = &$x.d1;
| ------ borrow of `db.d1` occurs here
...
16 | fn get_db<'a>() -> Database<'a> {
| -- lifetime `'a` defined here
...
19 | db
| ^^
| |
| move out of `db` occurs here
| returning this value requires that `db.d1` is borrowed for `'a`
error[E0515]: cannot return value referencing local data `db.d1`
--> src/main.rs:19:5
|
11 | $x.d2 = &$x.d1;
| ------ `db.d1` is borrowed here
...
19 | db
| ^^ returns a value referencing data owned by the current function
error[E0515]: cannot return value referencing temporary value
--> src/main.rs:19:5
|
17 | let mut db: Database = Database{ d1:"a".to_string(), d2:&"".to_string() };
| -------------- temporary value created here
18 | test!(db);
19 | db
| ^^ returns a value referencing data owned by the current function