I want an owned list of Rust trait objects. I could implement it as Vec<Box<dyn Trait>>
but that allocates space on the heap for every trait object. What I’d prefer is a CompactList<dyn Trait>
type with a memory representation that looks like:
[vtable1, size1, data1, vtable2, size2, data2, vtable3, size3, data3]
size*
is the size in bytes of the corresponding data*
.
With this, I could create an Iterator<Item = &dyn Trait>
. The only operations I need on CompactList<T>
are push()
and iter()
.
std::raw::TraitObject
. – mcartonunsafe
, rely on nightly features, and very likely to be undefined behavior. – Shepmastervtable ptr, size, data
, and then provide an iterator or index, that exposesraw::TraitObject
s. – Peter Hallunsafe
. – trent ᶠᵒʳᵐᵉʳˡʸ ᶜˡstd::raw::TraitObject
. – Calebmer