无法解析T:serde :: Deserialize<'a>在泛型结构上派生反序列化时 [英] Cannot resolve T: serde::Deserialize<'a> when deriving Deserialize on a generic struct

查看:177
本文介绍了无法解析T:serde :: Deserialize<'a>在泛型结构上派生反序列化时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写派生serde::Deserialize的结构,但它也具有应派生serde::Deserialize的字段:

I'm trying to write a struct that derives serde::Deserialize but it also has a field that should derive 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`

推荐答案

通常,您不应该在结构上写Serde特征界限..

rustc --explain E0283解释您的问题:

当编译器没有足够的信息来明确选择实现时,就会发生此错误

This error occurs when the compiler doesn't have enough information to unambiguously choose an implementation

我发现使用 #[serde(bound()] 声明边界可以使示例编译:

I've found that using #[serde(bound()] for declaring the bounds makes the example compile:

#[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,
}

作为另一种解决方案,由于T是通用的并且可能是引用,请考虑更改Record定义,以便Serde不需要更明确的指示:

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:

#[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,
        }
    }
}

这篇关于无法解析T:serde :: Deserialize&lt;'a&gt;在泛型结构上派生反序列化时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆