Scenegraph Structure with trait objects

Hello all, I've been "porting" a Project from C++ to- Rust.

Now I have got a Problem with my Scenegraph.
It uses a Trait called ScenegraphElement to store its Elements for Drawing.
This Trait allows to return childs that are also ScenegraphElements.

So far so good. This works, I can have a tree structure of ScenegraphElements in my SG.

My Problem is that, I cannot mutate these underlying objects anymore because these are
of course trait objects, stored in Vec<Rc<dyn ScenegraphElement>>

There is no (obvious) way for me to recover the underlying instance and mutate it.

The perfect solution would be to store them in the renderer (renderer holds scenegraph), as T, and let the SG only hold a Pointer to dyn ScenegraphElement. But I don't know how to do it (if even possible)

How can I structure my Program, so that I can store different Types that implement the ScenegraphElement-Trait, and be able to mutate them?

Thanks in advance :slight_smile:

Okay, I did some further research.
As far as I know, the solution I deemed to be perfect is just not.
I think it is far more ideomatic(and easier) to not have the scenegraph store anything.

Instead, I will just supply a Vec<&dyn ScenegraphElement> to the SG, whenever I want to draw.
This is perfect, because I would have to store Elements in the Renderer either way.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.