Deserialize to type given an id


#1

I have been trying to figure out how to use rustc_serialize to write to files. I am stuck on deserializing data to a type. If you ignore versioning issues and assume that you have a type id also stored with the data then is it possible to create a new instance from a “type pointer” given that type id? The idea that comes to mind is a Java map with the id as the key and the class as the object. I know rust does not do reflection, but can you do something similar? Maybe it has to be unsafe? I would like to be able to have a dynamic list of types, if possible. Any ideas?


#2

It’s sort of possible if you know all the possible types. Then you can simply use an enum where each type is a variant. Deserializing arbitrary types is significantly harder, since there is no TypeId to T mapping anywhere, that I know of. You can make one, yourself, but that’s as tedious as it sounds and I’m not sure if it works with rustc_serialize.