创建宏来简化深度嵌套枚举的声明?

发布于 2025-01-27 00:05:34 字数 802 浏览 1 评论 0原文

我想使用深度嵌套的枚举来表示我的游戏中的块:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2025-02-03 00:05:34

从构想中使用CDHOWIE的,我认为您只需要从最低级别的枚举中就需要特征。您可以跳过Imply off<材料>对于Element,因为您需要一个孩子来创建材料,因此从该级别开始确实没有意义。

impl From<Gas> for Element {
    fn from(e: Gas) -> Element {
        Element::Materal(Material::Gas(e))
    }
}

impl From<Liquid> for Element {
    fn from(e: Liquid) -> Element {
        Element::Materal(Material::NonGas(NonGas::Liquid(e)))
    }
}

impl From<ImmovableSolid> for Element {
    fn from(e: ImmovableSolid) -> Element {
        Element::Materal(Material::NonGas(NonGas::Solid(Solid::ImmovableSolid(e))))
    }
}

impl From<MovableSolid> for Element {
    fn from(e: MovableSolid) -> Element {
        Element::Materal(Material::NonGas(NonGas::Solid(Solid::MovableSolid(e))))
    }
}

fn main() {
    println!("{:?}", Element::from(ImmovableSolid::Bedrock));
}

Using cdhowie's From idea, I think you'd only need trait impls from your lowest level enums. You can skip ones like impl From<Material> for Element because you need a child to create a Material, so it doesn't really make sense to start at that level.

impl From<Gas> for Element {
    fn from(e: Gas) -> Element {
        Element::Materal(Material::Gas(e))
    }
}

impl From<Liquid> for Element {
    fn from(e: Liquid) -> Element {
        Element::Materal(Material::NonGas(NonGas::Liquid(e)))
    }
}

impl From<ImmovableSolid> for Element {
    fn from(e: ImmovableSolid) -> Element {
        Element::Materal(Material::NonGas(NonGas::Solid(Solid::ImmovableSolid(e))))
    }
}

impl From<MovableSolid> for Element {
    fn from(e: MovableSolid) -> Element {
        Element::Materal(Material::NonGas(NonGas::Solid(Solid::MovableSolid(e))))
    }
}

fn main() {
    println!("{:?}", Element::from(ImmovableSolid::Bedrock));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文