4

I'm attempting to create a sort of component system inspired by react for an OpenGL project I'm working on. This component system is defined by structs that contain some attributes and some functions. Like this:

pub struct Component<Lifecycle, PropType> {
    lifecycle: Lifecycle,
    props: PropType,
    children: Vec<Component<Lifecycle, PropType>>,
}

An example of Lifecycle looks like this:

pub struct MeshLifecycle {
    render: (Fn(ComponentProps) -> Mesh),
}

An example PropType looks like this:

pub struct ComponentProps {
    position: Vertex,
}

Now, this is resulting in some errors. First off:

the trait bound `std::ops::Fn(ComponentProps) -> Mesh + 'static: std::marker::Sized`

After some digging, I guess I'm supposed to apply ?Sized to types that contain functions essentially. So the Component definition now reads:

pub struct Component<Lifecycle: ?Sized, PropType> {

That partially helps, but doesn't get me quite there. Here is the full compiler error:

src/main.rs:10:5: 10:25 error: the trait bound `Lifecycle: std::marker::Sized` is not satisfied [E0277]
src/main.rs:10     lifecycle: Lifecycle,
                   ^~~~~~~~~~~~~~~~~~~~
src/main.rs:10:5: 10:25 help: run `rustc --explain E0277` to see a detailed explanation
src/main.rs:10:5: 10:25 help: consider adding a `where Lifecycle: std::marker::Sized` bound
src/main.rs:10:5: 10:25 note: only the last field of a struct or enum variant may have a dynamically sized type

Is there any way to make something like this work (and how), or is this too "dynamic" in nature? I read somewhere that what I'm essentially looking to do is create a "VTable". The various examples of that online don't seem to specify dynamically sized types so I'm wondering what I'm doing incorrectly.

Parris
  • 17,833
  • 17
  • 90
  • 133

0 Answers0