Reputation: 2879
I use a generic which is serializable and deserializable. However, there is an error on the Deserialize
derive attribute telling that the type can not be inferred.
The compile error is thrown for both struct and enum. Commenting one of those will not resolve anything.
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)
Upvotes: 6
Views: 2932
Reputation: 2079
You don't need the trait bounds. It is also generally unadvisable to do so on structs. This code works:
#[derive(Clone, Serialize, Deserialize)]
struct Jhon<A> {
foo: Foo<A>
}
#[derive(Clone, Serialize, Deserialize)]
enum Foo<A> {
A,
B(A)
}
Serde automatically tries to determine the bounds on Deserialize
and Serialize
implementations when you use its derive
macro implementation. To use custom bounds you need to tell Serde that you are doing this using the #[serde(bound = "...")]
attribute.
#[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)
}
Upvotes: 6
Reputation: 446
In addition to the answer given above, I should point out (since I ran into this myself today) that sometimes you already have the Deserialize bound satisfied in a supertrait the problematic type parameter, and this error represents serde trying and failing to supply its own (redundant) bound on that type parameter.
In that case you still want to use the serde(bound)
attribute but with an empty bound: that is, annotate with #[serde(bound = "")]
.
Upvotes: 4