gpt4 book ai didi

rust - 为具有 Deserialize trait bound 的泛型派生 Deserialize 时,无法推断类型参数的类型

转载 作者:行者123 更新时间:2023-12-03 11:25:45 51 4
gpt4 key购买 nike

我使用可序列化和可反序列化的泛型。但是,Deserialize上有错误派生属性告诉无法推断类型。

struct 和 enum 都会抛出编译错误。评论其中之一不会解决任何问题。

use serde::{Deserialize, Serialize}; // 1.0.104

#[derive(Serialize, Deserialize)]
struct Jhon<A>
where
A: Serialize + for<'a> Deserialize<'a>,
{
foo: Foo<A>,
}

#[derive(Serialize, Deserialize)]
enum Foo<A>
where
A: Serialize + for<'a> Deserialize<'a>,
{
A,
B(A),
}

error[E0283]: type annotations needed
--> src/lib.rs:3:21
|
3 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^ cannot infer type for type parameter `A`
|
= note: cannot resolve `A: _IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize<'de>`
= note: required by `_IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed
--> src/lib.rs:3:21
|
3 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^ cannot infer type for type parameter `A`
|
= note: cannot resolve `A: _IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize<'_>`
= note: required because of the requirements on the impl of `_IMPL_DESERIALIZE_FOR_Jhon::_serde::de::Visitor<'de>` for `_IMPL_DESERIALIZE_FOR_Jhon::<impl _IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize<'de> for Jhon<A>>::deserialize::__Visitor<'de, A>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed
--> src/lib.rs:11:21
|
11 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^ cannot infer type for type parameter `A`
|
= note: cannot resolve `A: _IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize<'de>`
= note: required by `_IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0283]: type annotations needed
--> src/lib.rs:11:21
|
11 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^ cannot infer type for type parameter `A`
|
= note: cannot resolve `A: _IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize<'_>`
= note: required because of the requirements on the impl of `_IMPL_DESERIALIZE_FOR_Jhon::_serde::de::Visitor<'de>` for `_IMPL_DESERIALIZE_FOR_Foo::<impl _IMPL_DESERIALIZE_FOR_Jhon::_serde::Deserialize<'de> for Foo<A>>::deserialize::__Visitor<'de, A>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

Playground

最佳答案

你不需要特征边界。通常也不建议在结构上这样做。此代码有效:

#[derive(Clone, Serialize, Deserialize)]
struct Jhon<A> {
foo: Foo<A>
}

#[derive(Clone, Serialize, Deserialize)]
enum Foo<A> {
A,
B(A)
}

Rust playground example

Serde 会自动尝试确定 Deserialize 的边界和 Serialize当您使用它时的实现 derive宏实现。要使用自定义边界,您需要使用 #[serde(bound = "...")] 告诉 Serde 您正在执行此操作。属性。
#[derive(Clone, Serialize, Deserialize)]
#[serde(bound = "A: Serialize, for<'a> A: Deserialize<'a>")]
struct Jhon<A> where A: Serialize + for<'a> Deserialize<'a>{
foo: Foo<A>
}

#[derive(Clone, Serialize, Deserialize)]
#[serde(bound = "A: Serialize, for<'a> A: Deserialize<'a>")]
enum Foo<A> where A: Serialize + for<'a> Deserialize<'a>, {
A,
B(A)
}

Rust playground example

关于rust - 为具有 Deserialize trait bound 的泛型派生 Deserialize 时,无法推断类型参数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61473323/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com