"cannot find value __collect" 将展平添加到枚举结构变体时

"cannot find value __collect" when adding flatten to an enum struct variant

我有以下代码:

extern crate serde;
#[macro_use]
extern crate serde_derive;

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Base {
    bold: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Component {
    String {
        #[serde(flatten)] base: Base,
        text: String,
    },
}

这给了我一个编译器错误:

error[E0425]: cannot find value `__collect` in this scope
  --> src/main.rs:10:28
   |
10 | #[derive(Debug, Serialize, Deserialize)]
   |                            ^^^^^^^^^^^ did you mean `__content`?

error[E0277]: the trait bound `<__S as serde::Serializer>::SerializeStruct: serde::ser::SerializeMap` is not satisfied
  --> src/main.rs:10:17
   |
10 | #[derive(Debug, Serialize, Deserialize)]
   |                 ^^^^^^^^^ the trait `serde::ser::SerializeMap` is not implemented for `<__S as serde::Serializer>::SerializeStruct`
   |
   = help: consider adding a `where <__S as serde::Serializer>::SerializeStruct: serde::ser::SerializeMap` bound
   = note: required because of the requirements on the impl of `serde::Serializer` for `serde::private::ser::FlatMapSerializer<'_, <__S as serde::Serializer>::SerializeStruct>`
   = note: required by `serde::Serialize::serialize`

如果我将我的代码更改为这样,它可以正常编译:

extern crate serde;
#[macro_use]
extern crate serde_derive;

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Base {
    bold: Option<bool>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Striii {
    #[serde(flatten)]
    base: Base,
    text: String,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Component {
    String(Striii),
}

我不明白编译器错误或为什么它不工作。 Serde 是否不支持枚举结构变体中的字段属性?

对于 serialization and deserialization,这是 Serde 的一个已知问题。没有列出解决方法。