then impl serde::Serializer for it... all that is fine.
But then it can never be used, because it's only recognized through the Serializer trait in serde::Serialize:
impl serde::Serialize for Destination {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
// There's no way to access the `config` state here!
}
}
&self cannot contain that state, unfortunately. That will complicate everything else in the program as it'll ruin many derives.
What's the proper way to solve this problem? Is there a solution for it that doesn't involve setting a global state?
As you already found out, self needs to carry all information. You don't need to implement Serialize on the struct itself, but create a helper struct like
Like I mentioned, unfortunately, this isn't an option. Destination (and others in a similar situation) are part of much bigger types, and we can't just pick and choose what to do. The whole grand, grand, grand... parent object, should be serialized, which will eventually lead to serializing Destination.
On the other hand, Config isn't compatible with all the properties of these types, which will result in issues with other derives.
I hope someone replies with a good solution, because I'd love to know, myself. I will just add two points from my own experience:
Use a thread local, not a normal global.
Be aware that some serde codecs rely on [de]serialization being effect-free, and can actually run over the data multiple times. You need to either be able to account for this, or avoid those formats.
Edit: bonus point, you can always implement your own serialization library. It's not a good solution, but can confirm it works wonders when you really need to pass extra context in.
Then rethink what you gonna do at all . For me the requirement is very unusual. Btw. do you also need to implement Deserialize?
In the end you can always do impl Serialize for MyParentParentParentWithConfig { .. } and call ChildWithConfig::new(&self.child, self.config).serialize(serializer)?; So you don't implement serde for your primary structs that don't know how to serialize themselves, but have a whole Serialize and Deserialize bunch of structs that does the job.