genericslinked-listruststatic-dispatch

How can I have static dispatch for a linked list containing different types all implementing a trait?


I have this working code:

struct Layer<'a> {
    parent: Option<Box<Layer<'a>>>,
    value: Box<dyn Renderable + 'a>,
}

I would like to have a version using static dispatch instead:

struct Layer<'a, R: Renderable> {
    parent: Option<&'a Layer<'a, /* ? */>>,
    value: R,
}

The type replacing the question mark implements Renderable, but it's not necessarily R, it could be T: Renderable for example. I would like to avoid any solution using dyn Renderable, to keep the static dispatch.

The type T: Renderable is known at Layer instantiation and won't change.


Solution

  • TL;DR: It is impossible (at least w/o variadic generics*)

    At the end, you have infinite recursion. Additional generic parameter for parent field have to be defined as a part of Layer structure. That induce introduction of a new generic parameter for Layer. That induce additional generic parameter for parent field.

    To break up recursion, additional generic parameter for parent shouldn't be a part of Layer definition. If a parameter is not a part of Layer definition then we can't calculate Layer size at compile time. The way it may be solved is &dyn or Box.

    *The other possible solution is variadic generics but it looks like we will not have it at least for next few months or even years.