创建宏来简化深度嵌套枚举的声明?
我想使用深度嵌套的枚举来表示我的游戏中的块:
enum Element { Void, Materal(Material) }
enum Material { Gas(Gas), NonGas(NonGas) }
enum NonGas { Liquid(Liquid), Solid(Solid) }
enum Solid { MovableSolid(MovableSolid), ImmovableSolid(ImmovableSolid) }
enum Gas { Smoke }
enum Liquid { Water }
enum ImmovableSolid { Bedrock }
enum MovableSolid { Sand, GunPowder }
我发现声明元素
>:
let block: Element = Element::Materal(Material::NonGas(NonGas::Solid(Solid::ImmovableSolid(ImmovableSolid::Bedrock))));
是否可以创建宏来添加句法糖来添加句法糖?
我希望创建一个可以自动解决枚举路径的宏
let block: Element = NewElement!(ImmovableSolid::Bedrock);
I want to use deeply nested enums to represent blocks in my game:
enum Element { Void, Materal(Material) }
enum Material { Gas(Gas), NonGas(NonGas) }
enum NonGas { Liquid(Liquid), Solid(Solid) }
enum Solid { MovableSolid(MovableSolid), ImmovableSolid(ImmovableSolid) }
enum Gas { Smoke }
enum Liquid { Water }
enum ImmovableSolid { Bedrock }
enum MovableSolid { Sand, GunPowder }
I found it very verbose to declare an Element
:
let block: Element = Element::Materal(Material::NonGas(NonGas::Solid(Solid::ImmovableSolid(ImmovableSolid::Bedrock))));
Is it possible to create a macro to add syntactic sugar for my enum declaration?
I'm hoping to create a macro that can automagically resolve the enum path, for example
let block: Element = NewElement!(ImmovableSolid::Bedrock);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从构想中使用CDHOWIE的
,我认为您只需要从最低级别的枚举中就需要特征。您可以跳过
Imply off<材料>对于Element
,因为您需要一个孩子来创建材料
,因此从该级别开始确实没有意义。Using cdhowie's
From
idea, I think you'd only need trait impls from your lowest level enums. You can skip ones likeimpl From<Material> for Element
because you need a child to create aMaterial
, so it doesn't really make sense to start at that level.