【发布时间】:2021-10-26 19:31:36
【问题描述】:
我想完全基于 Serialize 的自定义实现中的某些值跳过序列化
impl Serialize for MyType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match &self.value {
Action::Serialize => //serialize.serialize_struct( etc...
Action::NoSerialize => Ok(()) // <= Error. How do I return Ok and skip serialization ?
}
}
}
错误信息显示
mismatched types
expected associated type `<S as mystuff::_::_serde::Serializer>::Ok`
found unit type `()`rustcE0308
mod.rs(160, 22): consider constraining the associated type `<S as mystuff::_::_serde::Serializer>::Ok` to `()`: `<Ok = ()>`
但我不知道如何指定建议的约束<Ok = ()>
【问题讨论】: