Is it possible to store state within Rust's pr

2019-07-14 05:55发布

Is it possible to build a macro that doesn't output anything but instead stores state to build up a list and then a second macro that will then actually use that data?

For example:

trait SomeTrait {}

#[derive(mark)]
struct Person {}

impl SomeTrait for Person {}

#[derive(mark)]
struct Item {}

impl SomeTrait for Item  {}

#[derive(mark)]
struct Object {}

impl SomeTrait for Object {}

create_mapper! // this then outputs the below function
//assuming for the fact that data is loaded correctly before this macro is used

fn select_item(kind: String) -> impl SomeTrait {
    match kind {
        "person" => Person,
        "item" => Item,
        "object" => Object,        
    }
}

1条回答
太酷不给撩
2楼-- · 2019-07-14 06:29

No, currently it is not really possible to store state that can be used by two different proc macro invocations.

I created this very related issue where this problem is discussed.

It is certainly possible today in a hacky way, but there is no guaranteed way that will work. You could, for example, serialize all your state into /tmp/my-state. Or you could try using static global variables. But again, even if this works now, this is not guaranteed to work in the future. Another problem: due to incremental compilation, it is not guaranteed that all of your proc macro invocations are actually executed. So if you have one macro that generates the state and one that reads it, if the first is not executed, really strange things happen.

In the issue linked above, you can see that MSleepyPanda proposed a possible solution, but we are far from having this implemented.

查看更多
登录 后发表回答