#[derive(serde::Serialize, serde::Deserialize)]
#[serde(remote = "ExternalType")]
pub struct ExternalTypeWrapper {
...
}
#[derive(serde::Serialize, serde::Deserialize)]
struct X {
// works
#[serde(with = "ExternalTypeWrapper")]
x1: ExternalType,
// does not work
#[serde(with = "Vec::<ExternalTypeWrapper>")]
x2: Vec<ExternalType>,
}
为什么我的远程派生(反)序列化只对普通类型有效,但如果它被 Package 在类似Vec的东西中就不行了?这将导致以下错误:
the function or associated item `serialize` exists for struct `Vec<ExternalTypeWrapper>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`ExternalTypeWrapper: Serialize`
which is required by `Vec<ExternalTypeWrapper>: Serialize`
`Vec<ExternalTypeWrapper>: Serialize`
which is required by `&Vec<ExternalTypeWrapper>: Serialize`
`Vec<ExternalTypeWrapper>: Serialize`
which is required by `&mut Vec<ExternalTypeWrapper>: Serialize`
(与Deserialize trait类似)
1条答案
按热度按时间mwg9r5ms1#
这是不可能的只有serde。有一个关于它的old bug report,但serde维护者没有解决方案。你可以使用
serde_with
来解决这个问题。用户指南中解释了这些步骤。使用两个trait
SerializeAs
/DeserializeAs
,它们的工作方式类似于serde trait,您可以添加对嵌套类型的支持,如Option
或Vec
。然后使用serde_as
而不是serde(with = "...")
注解结构体和字段。Solution on Rustexplorer