← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 6074.d.1.a.3.c.12.a.1.j.1.a.2.b.1 · 生成模型:GPT-6

声明宏生成过程宏样板

理解等级:中级

过程宏 crate 的内部也可用 macro_rules! 减少实现样板。书中把各 SDK 操作的错误类型匹配为 ty,把对应的 IacError 变体构造器匹配为 expr。声明宏生成 From<SdkError<...>> for IacError 的实现,不同调用只需给出变化项。错误转换发生在过程宏处理职责内,生成转换实现则发生在编译这个宏 crate 时。

同一种组合能服务不同入口。Rocket 用声明宏定义多种同形的属性过程宏,SQLx 让公共 query! 包装内部的过程宏入口,后者承担解析、展开和错误输出。Shuttle 的例子还在声明宏内部调用 paste! 生成相关代码。

相关卡片 [[外部操作错误进入编译诊断]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Writing an infrastructure DSL 把服务错误变成编译诊断 · 查看原书依据

      And nothing is stopping us from using additional declarative or procedural macros within a macro—though the latter would require setting up another project. In this case, a declarative macro will do just fine. We only need to pass in two things: the variant for our error enum (expr will do, though path is also a good candidate) and the type of the AWS error (ty, as you may recall from way back).

    参考来源

    - 位置:《Write Powerful Rust Macros》第09章《Writing an infrastructure DSL》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-9.xhtml#p155) - 结论依据:原文说明过程宏内部也可以使用其他宏,清单 9.25 以两个变化项复用 From 实现,9.9 进一步展示不同的调用关系。 - 原始内容:And nothing is stopping us from using additional declarative or procedural macros within a macro—though the latter would require setting up another project. In this case, a declarative macro will do just fine. We only need to pass in two things: the variant for our error enum (expr will do, though path is also a good candidate) and the type of the AWS error (ty, as you may recall from way back).