The nested struct needs to implement the Serialize and Deserialize traits too. As you can see, I can require that P implements the Serialize trait without problem, but I'm struggeling to add a similar requirement for the Deserialize trait
As soon as I add:
pub struct Command<P> where P: Serialize + Deserialize {
I get this error:
expected named lifetime parameter
help: consider making the bound lifetime-generic with a new `'a` lifetime
But this won't work either:
pub struct Command<'a, P> where P: Serialize + Deserialize<'a> {
...because of:
error[E0283]: type annotations needed: cannot satisfy `P: Deserialize<'_>`
Also, why does my "outer" struct suddenly need a life-time parameter when it didn't before?
Actually I don't want the "outer" struct to have that additional life-time parameter, if possible...
error[E0277]: the trait bound `P: DeserializeOwned` is not satisfied
the trait `for<'de> Deserialize<'de>` is not implemented for `P`
note: required for `P` to implement `DeserializeOwned`
But then how do I prevent my "outer" (generic) struct to be instantiated with an "inner" sturct that doesn't impelemnt Serialize or that doesn't implement Deserialize?
I think, without that check, my "outer" struct may suddenly fail (for not so obvisous reasons) when one attempts to pass it to a function that requires the deserialize functionality...
You don't prevent it at creation time, but it won't compile as soon as it's used in any code with a Deserialize bound. This is till a compile-time guarantee.
This is generally the approach taken by types in Rust. Otherwise the generic bounds need to be repeated in many places that don't really need them.