← 返回起始笔记位置

知识卡片

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

派生辅助属性配置展开

理解等级:中级

派生辅助属性给生成器额外输入。例如 #[proc_macro_derive(Builder, attributes(rename, builder_defaults))] 一次注册两个名字。生成器扫描 DeriveInput.attrs 读取整项的配置,扫描 Field.attrs 读取单个字段的配置。辅助属性是惰性的。派生宏用它决定追加哪些内容,而原输入仍由编译器保留。只有声明名字还不够,生成器也要实现相应的解析和行为。

相关卡片 [[从字段生成构建器代码]]、[[属性宏的两个输入]]。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Write Powerful Rust Macros》第08章《Builder with attributes》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-8.xhtml#p16) - 结论依据:清单 8.1–8.2 展示如何注册字段上的 rename 并以展开输出核实属性继续存在。8.3 在整个结构体层放置默认值配置。 - 原始内容:A derive macro’s attributes are called “derive macro helper attributes.” They are inert, meaning they will not remove themselves during attribute processing. You can see this for yourself by running cargo expand on a derive macro with attributes—the output still contains the attributes.