Here's what I want to do:
struct Foo<T: Trait> {
inner: T,
}
struct Bar<'a> {
foo: &'a Foo<dyn Trait>, // This is pseudo code, I don't think Rust has this feature.
}
I want an instance of Bar
to hold a reference to a Foo
where the type parameter of Foo
is dynamic, i.e. over the lifetime of a single Bar
it may hold different instances of Foo
with different types for T
. Since Bar
only holds a reference, it would still be Sized
. As far as I can tell this is impossible right now. If this were possible, then I could do it multiple times in the same type, i.e.
struct Foo<T: Trait, U: Trait> {
a: T,
b: U,
}
struct Bar<'a> {
foo: &'a Foo<dyn Trait, dyn Trait>,
}
Now foo
needs to hold at least three pieces of information: a pointer, a pointer to a vtable for T, and a pointer to a vtable for U. This is unlike references to DSTs as they currently exist in the language, which hold two pieces of information: &[T]
holds a pointer and a length, and &dyn Trait
which holds a pointer and a pointer to a vtable. I think this could be feasible, but as far as I'm aware nothing like this exists in Rust at the moment. So, what's the closest thing to what I want to do?
Actually, on second thought, foo
in the above example could just hold one vtable pointer, and we could have a separate vtable for every combination of T
and U
. So maybe this is possible right now??