shipyard icon indicating copy to clipboard operation
shipyard copied to clipboard

feature request: TraitView

Open dakom opened this issue 2 years ago • 0 comments

This was discussed on Zulip, but isn't being actively worked on at the moment, so opening a tracking issue with @leudz blessing. I have no idea how difficult it would be to add this, might be super hard!

Motivation:

Let's say we're building a renderer where we don't know all the possible variants of the meshes/materials, but we do know they satisfy a certain interface. It would be wonderful to do something like this:

pub fn render_sys(renderer: ViewMut<MyRenderer>, meshes: TraitView<&dyn Mesh>, materials: TraitView<&dyn Material>) {
    for (mesh, material) in (&meshes, &materials).iter() {
        // assume Mesh is a trait with a draw() method
        // that takes a context like OpenGl / WebGPU /etc.
        // and a &dyn Material
        mesh.draw(&mut renderer.context, material);
    }
}

This is doable without any heap allocation in an application by having the Mesh/Material be enums, though this requires all the variants be defined in a rigid manner.

If the above renderer were used as a library, then downstream code could merely add Mesh and Material components that satisfy the trait. Much more flexible!

Prior art:

https://github.com/BoxyUwU/bevy_unofficial_trait_queries

dakom avatar Jul 24 '22 09:07 dakom