我有以下代码:
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 是否不支持枚举结构变体中的字段属性?