Explanation for this code please

Are basically you asking why you need

impl<A, B> Serialize for (A, B)
where
    A: Serialize<Nominative=A> + Debug,
    B: Serialize<Nominative=B> + Debug,
    //           ^^^^^^^^^^^^

in this thread?

It's because the implementation has to work for all types A and B that meet the bound -- including all possible future implementations, not just the current ones. So if you need to rely on some sort of type equality, you need to put it into the bound.

E.g. in that other thread, there's nothing preventing an implementation of Serialize where the associated Nominative type is not Self.


If you want some implementations for a fixed set of types, and want to rely on details of those implementations that aren't expressible as trait bounds,[1] sometimes macros to create a bunch of implementations on the specific types are a better approach than generic implementations.


  1. or are just annoying to express that way ↩︎