Hi,
I am trying to parse a file that contains serialized C++ objects, and represent them within Rust. The file starts with a list of every object, then the objects themselves can also refer to one another. Ideally, I think I'd like something like this:
struct A {
// Only simple types
foo: u32,
bar: f32,
}
struct B<'a> {
// Mix of simple types and references
baz: u32,
boom: &'a RefCell<A>,
}
enum Classes {
A(RefCell<A>),
B(RefCell<B>),
}
struct MyFile {
objects: Vex<Classes>,
}
That is:
- The structure representing the file owns the 'real' list of deserialized objects
- Object types that reference other objects have references to the real version in the file's structure, with RefCells for runtime borrow-checking enforcement (let's say this is all single-threaded, or switch the RefCell for a Mutex)
- Therefore any modifications to an object, either through the master list in the file's structure, or through a different object within the file by accessing its members, modifies the object stored in the file's structure.
I am really struggling with this however. I can't work out:
- How to initially construct the file structure. If B needs a reference to A, as we're looping through the objects maybe A hasn't been made yet.
- How to have B hold a reference to the concrete type, which we know, rather than the enum type.
The enum is not required, but I don't know how else to have a big collection of different object types. I don't think a trait is appropriate, because I want users to be able to access the unique fields of each object.
Thanks for any help! This feels like something that'd be better to do in say C++, but so much of the rest of the project is working really well in Rust (macros to generate the class definitions, compiling to wasm) that it'd be a real shame to have to give up on it now