【问题标题】:Does serde skip attribute actually skip an enum variant?serde 跳过属性实际上会跳过枚举变体吗?
【发布时间】:2020-06-03 14:30:19
【问题描述】:

我有一个像这样的枚举:

#[derive(Debug, Deserialize, Serialize)]
enum E {
    A(i32),

    #[serde(skip)]
    B(bool),

    C(char),
    D(Vec<i32>),
}

然后我尝试使用bincode crate 执行以下操作:

fn main() {
    let data = E::C('A');
    let encoded = bincode::serialize(&data).unwrap();
    let decoded = bincode::deserialize::<E>(&encoded).unwrap();
    println!("{:?}", decoded);
}

然而,这会引发以下消息:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Custom { kind: UnexpectedEof, error: "failed to fill whole buffer" })', src/main.rs:16:19

我注意到,如果满足以下条件之一,一切正常:

  1. 我删除了#[serde(skip)] 属性
  2. 我从变体中删除元组

我也知道 bincode 会以某种方式忽略 #[serde(skip)] 并尝试将 encoded 反序列化为 E::D(Vec&lt;i32&gt;)。如果我将Vec&lt;i32&gt; 更改为char 它将起作用,但decoded 将是E::D('A')(而不是E::C('A'))。

我错过了什么还是 bincode crate 中的错误?

【问题讨论】:

  • 这看起来像是bincode 中的一个错误。
  • 这似乎是这个问题:github.com/servo/bincode/issues/184
  • 感谢您的评论!这是一个悲伤的消息; bincode 已经到了 1.0.0 但是有一个烦人的错误。 :(

标签: rust serde bincode


【解决方案1】:

目前看来,跳过带有serde 的字段在bincode 等非自描述格式上效果不佳。有几个未解决的问题:

【讨论】:

    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多