英文:
Does syn::Variant have a mechanism to specify variant visibility?
问题
我有一个生成枚举及其变体的过程宏,并希望为其添加可配置的可见性,但似乎 syn::Variant 结构体没有可见性字段。由于它可以解析带有可见性的变体,所以我不确定是否有其他机制可以让我设置可见性,我可能错过了什么。
是否有其他设置可见性的方法,或者这是一个错误?
英文:
I have a procedural macro that generates an enum plus its variants and I'd like to add configurable visibility to it, but it looks like the syn::Variant struct doesn't have a visibility field. For some reason it can parse a variant with a visibility so I'm not sure if there's another mechanism for me to set the visibility that I've missed.
Is there another way to set the visibility, or is this a bug?
答案1
得分: 2
抱歉,这是按设计要求的。
https://doc.rust-lang.org/reference/items/enumerations.html#variant-visibility 中提到:“枚举变体在语法上允许可见性注释,但在验证枚举时会被拒绝。这允许在不同上下文中统一语法下解析项目。”
此外,syn
在解析enum
变体时忽略了可见性:https://github.com/dtolnay/syn/blob/bf7774b10555bd24a14008ad0c46d6ebde202a1c/src/data.rs#LL166C28-L166C28
英文:
Unfortunately, this is by design.
https://doc.rust-lang.org/reference/items/enumerations.html#variant-visibility says: "Enum variants syntactically allow a Visibility annotation, but this is rejected when the enum is validated. This allows items to be parsed with a unified syntax across different contexts where they are used."
Also, syn
ignores the visibility when parsing an enum
variant: https://github.com/dtolnay/syn/blob/bf7774b10555bd24a14008ad0c46d6ebde202a1c/src/data.rs#LL166C28-L166C28
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论