Is it possible to store state within Rust's procedural macros?

▼魔方 西西 提交于 2019-12-01 19:35:18

Currently there is no officially supported way to store state that can be used by two different proc macro invocations. I created this very related issue where this problem is discussed.

Storing state is certainly possible, but just in a hacky way. You could, for example, serialize all your state into /tmp/my-state. Or you could try using static global variables. But 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. So it is technically possible to store global state, but it's not advisable.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!