I want to use trait objects in a Vec
. In C++ I could make a base class Thing
from which is derived Monster1
and Monster2
. I could then create a std::vector<Thing*>
. Thing
objects must store some data e.g. x : int, y : int
, but derived classes need to add more data.
Currently I have something like
struct Level {
// some stuff here
pub things: Vec<Box<ThingTrait + 'static>>,
}
struct ThingRecord {
x: i32,
y: i32,
}
struct Monster1 {
thing_record: ThingRecord,
num_arrows: i32,
}
struct Monster2 {
thing_record: ThingRecord,
num_fireballs: i32,
}
I define a ThingTrait
with methods for get_thing_record()
, attack()
, make_noise()
etc. and implement them for Monster1
and Monster2
.
Trait objects
The most extensible way to implement a heterogeneous collection (in this case a vector) of objects is exactly what you have:
Although there are times where you might want a lifetime that's not
'static
, so you'd need something like:You could also have a collection of references to traits, instead of boxed traits:
An example:
Box<SomeTrait>
,Rc<SomeTrait>
,&SomeTrait
, etc. are all trait objects. These allow implementation of the trait on an infinite number of types, but the tradeoff is that it requires some amount of indirection and dynamic dispatch.See also:
Enums
As mentioned in the comments, if you have a fixed number of known alternatives, a less open-ended solution is to use an enum. This doesn't require that the values be
Box
ed, but it will still have a small amount of dynamic dispatch to decide which concrete enum variant is present at runtime: