解构 Rust 枚举而无需匹配

huangapple go评论60阅读模式
英文:

Destructure rust enum without matching

问题

以下是代码的翻译部分:

impl<'a> Debug for AstNode<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AstNode::Literal(inner) => inner.fmt(f),
            AstNode::Variable(inner) => inner.fmt(f),
            AstNode::Binary(inner) => inner.fmt(f),
            AstNode::Unary(inner) => inner.fmt(f),
            AstNode::Call(inner) => inner.fmt(f),
            AstNode::Function(inner) => inner.fmt(f),
            AstNode::If(inner) => inner.fmt(f),
            AstNode::While(inner) => inner.fmt(f),
            AstNode::Begin(inner) => inner.fmt(f),
            AstNode::Assign(inner) => inner.fmt(f),
            AstNode::NewGlobal(inner) => inner.fmt(f),
            AstNode::Error(inner) => inner.fmt(f),
            AstNode::TestAssert(inner) => inner.fmt(f),
            AstNode::TestExpect(inner) => inner.fmt(f),
        }
    }
}

如果您需要更多的帮助,请随时提出。

英文:

What would be the correct way to simplify this strange code? There is no variant of AstNode that doesn't wrap around an inner structure.

impl&lt;&#39;a&gt; Debug for AstNode&lt;&#39;a&gt; {
    fn fmt(&amp;self, f: &amp;mut std::fmt::Formatter&lt;&#39;_&gt;) -&gt; std::fmt::Result {
        match self {
            AstNode::Literal(inner) =&gt; inner.fmt(f),
            AstNode::Variable(inner) =&gt; inner.fmt(f),
            AstNode::Binary(inner) =&gt; inner.fmt(f),
            AstNode::Unary(inner) =&gt; inner.fmt(f),
            AstNode::Call(inner) =&gt; inner.fmt(f),
            AstNode::Function(inner) =&gt; inner.fmt(f),
            AstNode::If(inner) =&gt; inner.fmt(f),
            AstNode::While(inner) =&gt; inner.fmt(f),
            AstNode::Begin(inner) =&gt; inner.fmt(f),
            AstNode::Assign(inner) =&gt; inner.fmt(f),
            AstNode::NewGlobal(inner) =&gt; inner.fmt(f),
            AstNode::Error(inner) =&gt; inner.fmt(f),
            AstNode::TestAssert(inner) =&gt; inner.fmt(f),
            AstNode::TestExpect(inner) =&gt; inner.fmt(f),
        }
    }
}

答案1

得分: 1

如果inner成员不依赖于枚举变体,那么无需将其放入枚举中。您可以将其提升,因此无需匹配它。大致如下:

struct AstNode(AstKind, InnerType);

enum AstKind {
  Literal,
  Variable,
  ...
}

然后,您可以编写:

impl<'a> Debug for AstNode<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.1.fmt(f)
    }
}

这种类型系统明确声明了InnerTypeAstKind是独立的。如果我理解正确的话。

英文:

If the inner member is not dependent on enum variant there is no need to put it in enum. You can promote it up, so there will be no need to match it. Something along those lines:

struct AstNode(AstKind, InnerType);

enum AstKind {
  Literal,
  Variable,
  ...
}

then you can write:

impl&lt;&#39;a&gt; Debug for AstNode&lt;&#39;a&gt; {
    fn fmt(&amp;self, f: &amp;mut std::fmt::Formatter&lt;&#39;_&gt;) -&gt; std::fmt::Result {
        self.1.fmt(f)
    }
}

This type system explicitly states that InnerType and AstKind is independent. Which is your case If I understand correctly.

huangapple
  • 本文由 发表于 2023年2月6日 13:21:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定