0

I'm attempting to write a library that allows the implementor to add a single instance of an arbitrary type to a list of similar type instances. I would like for the implementor to be able to retrieve this instance by type. For example: list_of_types.get::<MyType>().

I have achieved this with an AnyMap, but the next thing I want to do is give the implementor the ability to define a list of types that can be looped through and used to access elements of this AnyMap. Does Rust have a way to store types themselves rather than instances? Does it change anything if these arbitrary types implement the same trait? Is there a better way to handle a list accessible by element type?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
MantaRoll
  • 1
  • 1
  • 1
    You can use `std::any::TypeId` as a map key. Does that help? – Peter Hall Feb 19 '19 at 15:57
  • 4
    It might help to explain the problem if you wrote some more complete code of how you expect a client of this library to interact with it. – Peter Hall Feb 19 '19 at 16:02
  • 1
    From [this](https://stackoverflow.com/a/25247480/7512448) answer: "Such is the nature of the type IDs being used internally: unlike in a dynamic or VM-based language, the type system is purely a compile-time construct; there is no such thing as the type system at runtime.". – Zonico Feb 19 '19 at 18:35

1 Answers1

0

As suggested in the comments, I would check out core::any::TypeId.

I am not sure though what you are trying to accomplish. From what I understood, you want to supply a list of types and get the associated values.

I don't think that there is a way in Rust to supply a variable number of type arguments, but I can think of two ways how you could do it:

The first but more difficult/complicated one is that you write a macro which takes a list of types and, for every supplied type, takes the value of this type and accumulates those values in a Vec.

A simpler solution would be writing a function that directly takes an array of TypeIds and looks up the associated values of these ids and returns them. This is basically the above solution but the user has to take care of the boilerplate and the obtaining of the types' TypeIds so there is more friction.

There is also a crate called shred that you could use to get some help. It includes a type Resources which contains a map of some TypeId and maps to a value of the respective type. I think this is approximately what you want to do.

If you struggle implementing your project, I would suggest you take a look at how they implemented their Resources type in the source code.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Zonico
  • 192
  • 1
  • 11
  • *a function that directly takes an array of TypeIds and looks up the associated values of these ids and returns them* -- returns them as *what*? A massive `enum`? `&Any`? Accumulating them in a `Vec` has the same problem: what is `T`? – trent Feb 19 '19 at 19:03
  • You'd have to return them as some pointers to Any in the same order as the `TypeId`s came in. I think this problem can't be circumvented and is inert to the asked question. – Zonico Feb 20 '19 at 17:10