简体   繁体   English

在通用结构上派生反序列化时,无法解析T:serde :: Deserialize <'a>

[英]Cannot resolve T: serde::Deserialize<'a> when deriving Deserialize on a generic struct

I'm trying to write a struct that derives serde::Deserialize but it also has a field that should derive serde::Deserialize : 我正在尝试编写一个派生serde::Deserialize的结构,但它也有一个应该派生serde::Deserialize的字段:

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

use serde::{Deserialize, Serialize};

#[derive(PartialEq, Serialize, Deserialize)]
pub struct Record<'a, T>
where
    T: 'a + Serialize + Deserialize<'a>,
{
    id: &'a str,
    created_at: &'a str,
    created_by: Option<&'a str>,
    last_updated_at: Option<&'a str>,
    object: &'a T,
}

impl<'a, T> Record<'a, T>
where
    T: 'a + Serialize + Deserialize<'a>,
{
    pub fn new(
        id: &'a str,
        created_at: &'a str,
        created_by: Option<&'a str>,
        last_updated_at: Option<&'a str>,
        object: &'a T,
    ) -> Self {
        Record {
            id,
            created_at,
            created_by,
            last_updated_at,
            object,
        }
    }
}

fn main() {}

I've been changing the code for a while but I can't get this idea to compile. 我已经改变了一段时间的代码,但我无法将这个想法编译。 The error I'm getting at the moment is: 我现在得到的错误是:

error[E0283]: type annotations required: cannot resolve `T: serde::Deserialize<'a>`
 --> src/main.rs:7:32
  |
7 | #[derive(PartialEq, Serialize, Deserialize)]
  |                                ^^^^^^^^^^^
  |
  = note: required by `serde::Deserialize`

In general, you should not write Serde trait bounds on structs . 通常, 您不应该在结构上编写Serde特征边界

rustc --explain E0283 explains your problem: rustc --explain E0283解释你的问题:

This error occurs when the compiler doesn't have enough information to unambiguously choose an implementation 当编译器没有足够的信息来明确选择实现时,会发生此错误

I've found that using #[serde(bound()] for declaring the bounds makes the example compile: 我发现使用#[serde(bound()]来声明边界使得示例编译:

#[derive(PartialEq, Serialize, Deserialize)]
pub struct Record<'a, T: 'a> {
    id: &'a str,
    created_at: &'a str,
    created_by: Option<&'a str>,
    last_updated_at: Option<&'a str>,
    #[serde(bound(deserialize = "&'a T: Deserialize<'de>"))]
    object: &'a T,
}

As another solution, as T is generic and may be a reference, consider changing the Record definition so Serde does not need more explicit indication: 作为另一种解决方案,由于T是通用的并且可能是参考,因此请考虑更改Record定义,以便Serde不需要更明确的指示:

#[derive(PartialEq, Serialize, Deserialize)]
pub struct Record<'a, T: 'a> {
    id: &'a str,
    created_at: &'a str,
    created_by: Option<&'a str>,
    last_updated_at: Option<&'a str>,
    object: T,
}

impl<'a, T: 'a> Record<'a, T> {
    pub fn new(
        id: &'a str,
        created_at: &'a str,
        created_by: Option<&'a str>,
        last_updated_at: Option<&'a str>,
        object: T,
    ) -> Self {
        Record {
            id,
            created_at,
            created_by,
            last_updated_at,
            object,
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Serialize和DeserializeOwned作为超级特征时,无法解析`T:serde :: Deserialize &lt;&#39;de&gt;` - Cannot resolve `T: serde::Deserialize<'de>` when using Serialize and DeserializeOwned as supertraits 为具有反序列化特征绑定的泛型派生反序列化时无法推断类型参数的类型 - Cannot infer type for type parameter when deriving Deserialize for a type with a generic with a Deserialize trait bound 派生Serde的序列化或反序列化强制泛型类型是可序列化的,尽管它不需要 - Deriving Serde's Serialize or Deserialize forces generic type to be serialisable although it does not need to be 我无法将 json String 反序列化为 serde 结构 - I can not deserialize json String to struct with serde 如何使用 serde_yaml 在结构中反序列化这个 YAML - How to deserialize this YAML in struct with serde_yaml 如何为结构实现反序列化? (Serde 和 SerdeJSON) - How to implement Deserialize for a struct? (Serde and SerdeJSON) 如何使用 serde 将 JSON 数组反序列化为结构? - How to deserialize a JSON array into a struct using serde? 通过实现 serde `Deserializer` 将值的 Vec 反序列化为结构 - Deserialize a Vec of values into a struct by implementing serde `Deserializer` 使用 serde,是否可以反序列化为实现类型的结构? - Using serde, is it possible to deserialize to a struct that implements a type? 带有反序列化和通用生命周期的 serde_json function - serde_json with deserialize and lifetimes for generic function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM