如何查看 Parity Substrate 中宏生成的最终代码?
How can I look at the final code generated by the macros in Parity Substrate?
Substrate 使用了很多宏来让编写运行时模块变得更容易:
construct_runtime!
decl_module!
decl_storage!
decl_event!
- 等...
但是,很难理解这些宏的实际作用以及最终代码的样子。我怎样才能更深入地研究这些宏和扩展?
如果您想查看 crate 的最终生成代码,您可以运行以下内容:
cargo +nightly rustc --profile=check --package <crate-name> --lib -- -Zunstable-options --pretty=expanded > <output-file>
注意这里有两个变量:<crate-name>
和 <output-file>
。
因此,如果您想查看 substrate-node-template 的最后 运行 时间,您可以 运行:
cargo +nightly rustc --profile=check --package node-template-runtime --lib -- -Zunstable-options --pretty=expanded > substrate-node-template-runtime.rs
或者如果你只想查看像 Sudo 模块这样的单个模块的扩展,你可以这样做:
cargo +nightly rustc --profile=check --package srml-sudo --lib -- -Zunstable-options --pretty=expanded > sudo-module.rs
这些将生成包含所有扩展代码的文件,如下所示:https://gist.github.com/shawntabrizi/b4a1952dbd3af113e8a3498418e52741
对我来说,最舒服的解决方案是使用 cargo expand
https://github.com/dtolnay/cargo-expand
cargo install cargo-expand
然后从你的 crate 中调用它
cargo expand
Substrate 使用了很多宏来让编写运行时模块变得更容易:
construct_runtime!
decl_module!
decl_storage!
decl_event!
- 等...
但是,很难理解这些宏的实际作用以及最终代码的样子。我怎样才能更深入地研究这些宏和扩展?
如果您想查看 crate 的最终生成代码,您可以运行以下内容:
cargo +nightly rustc --profile=check --package <crate-name> --lib -- -Zunstable-options --pretty=expanded > <output-file>
注意这里有两个变量:<crate-name>
和 <output-file>
。
因此,如果您想查看 substrate-node-template 的最后 运行 时间,您可以 运行:
cargo +nightly rustc --profile=check --package node-template-runtime --lib -- -Zunstable-options --pretty=expanded > substrate-node-template-runtime.rs
或者如果你只想查看像 Sudo 模块这样的单个模块的扩展,你可以这样做:
cargo +nightly rustc --profile=check --package srml-sudo --lib -- -Zunstable-options --pretty=expanded > sudo-module.rs
这些将生成包含所有扩展代码的文件,如下所示:https://gist.github.com/shawntabrizi/b4a1952dbd3af113e8a3498418e52741
对我来说,最舒服的解决方案是使用 cargo expand
https://github.com/dtolnay/cargo-expand
cargo install cargo-expand
然后从你的 crate 中调用它
cargo expand