Description
The title is a mouthful, so let me explain and give some motivation. Say I have a trait MyTrait
with an associated type MessageType
. This message type must be deserializable (in the serde sense), i.e., MessageType: for<'a> Deserialize<'a>
. Now suppose I have a generic struct GenericStruct<T>
where T: MyTrait
. I want the struct to contain a message, so I put a thing of type T::MessageType
inside the struct. I would like GenericStruct
to also be Deserialize
able now, so I #[derive(Deserialize)]
.
Concretely, the code
use serde::Deserialize;
//trait DeserializeOwned: for<'a> Deserialize<'a> {}
trait MyTrait {
type MessageType: Sized + for<'a> Deserialize<'a>;
//type MessageType: Sized + DeserializeOwned;
}
#[derive(Deserialize)]
struct GenericStruct<T: MyTrait>(T::MessageType);
compiles in 1.31.1-stable and fails in 1.33.0-nightly with error message
error[E0308]: mismatched types
|
| #[derive(Deserialize)]
| ^^^^^^^^^^^ one type is more general than the other
|
= note: expected type `for<'a> _IMPL_DESERIALIZE_FOR_GenericStruct::_serde::Deserialize<'a>`
found type `_IMPL_DESERIALIZE_FOR_GenericStruct::_serde::Deserialize<'de>`
Furthermore, if the commented lines are uncommented, and the first type
line in MyTrait
is commented out, the code now compiles in stable and nightly.
I don't believe this is version-specific behavior in serde. I would like to make a neater minimal testcase, but I don't know of any auto-derivable traits that only take a lifetime as a parameter.