While I was trying this, it seemed like not a very clean solution, at least for me. If it were a relatively small struct that would be one thing, but my real struct has about 50 fields, one or two of which are pretty large in size (possibly 100s of MB).
The first issue was I couldn't find a clean way of moving all fields with like names from StructA to StructB. Then I was also thinking those two large fields would probably have their contents moved around in memory if I'm just doing:
The large fields should be heap allocated then to not exceed any stack limits when using the struct. I think if you put the large fields each in a Box<...> I think rust will handle that for you. If you do that then moving the data to the new struct will effectively just be copying the pointer. That said, I’ve never used serde with a Box field so maybe there is a limitation I’m not aware of.
Another approach to dealing with the two fields may just be to define a helper function that returns the desired result from the two field. And maybe a setter function that sets the fields as well.
Never used serde myself before but I read the docs for an hour or so and tried to find a reasonable approach. Here’s what I came up with, hope you might find it useful: