How do I process enum/struct/field attributes in a procedural macro?

前端 未结 2 1402
不知归路
不知归路 2021-01-04 03:31

Serde supports applying custom attributes that are used with #[derive(Serialize)]:

#[derive(Serialize)]
struct Resource {
    // Always serializ         


        
2条回答
  •  -上瘾入骨i
    2021-01-04 04:26

    You implement attributes on fields as part of the derive macro for the struct (you can only implement derive macros for structs and enums).

    Serde does this by checking every field for an attribute within the structures provided by syn and changing the code generation accordingly.

    You can find the relevant code here: https://github.com/serde-rs/serde/blob/master/serde_derive/src/internals/attr.rs

提交回复
热议问题