The exact thing you are asking for is impossible because the relative layout of the fields differs between the structs: in particular the relative position in memory of the a and c fields (likely[1]) differs between ABC and AC. Of course, this code is obviously either artificial or some sort of simplification or a real use-case, and presumably supposed to serve some purpose, i.e. aids with solving some underlying problem. If you tell us more about the context, we can discuss what alternative approaches that are possible you could take.
assuming a straightforward layout, though to be precise, the exact layout for ordinary structs in Rust is not really well-defined at all, and one would also need to use things like e.g. repr(C) to reason about actual specific layouts of types ↩︎
The more complete context is that I have a struct that I want to save across multiple files when persisting to disk, but when operating in the program it makes sense to have everything together.
e.g.
In the program I would like to keep the Person struct together for ergonomics, but when persisting to disk the Identity.name and Identity.social_security should be stored separately for e.g. security reasons, but both should be indexed by Identity.id.
The you can just create and return view structs by-value, which themselves contain individual references to each field. &T: Serialize if T: Serialize, so the derive macro should work just fine.
I've been playing with your suggestion, but am struggling to implement it because of the signature of Persist.data. Would I need to have two structs to handle the lifetime and owned version?
I am also struggling because the Persist trait actually enforces trait Persist<T> where T: Serialize + DeserializeOwned. Do I need to change DeserialzeOwned to Deserialize?