Circular borrowing

I think I've got a special case where I'm trying to borrow the same variable both as immutable and mutable at the same time due to a kind of circular reference. I know this is not a good programming practice, but in the context I'm working with it makes sense.

An example of the code is here:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=71e5b3cca36478df771ae221cb354e9e

The real scenario is a bit different as the MainLoopContext is being sent down the Scene object through that process function because I have an array of systems that use that MainLoopContext object for various computations, but that context lives outside the library and it can be defined by the end user in his own application. Thus it's implemented as a generic.
The problem is that MainLoopContext has to hold a reference to the Scene object because that is also used by user-defined systems.

Is there another way around this? Thanks in advance!

Split your context in two, 1 containing part containing the scene, the other part containing all other data nneded for process then pass the other data to process directly.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=42c49985118ae2e8b600fa546c3443cd

That would be a good solution if I didn't need to access both the Data and Scene struct from within that process function, which is my real use case :frowning:

Why not make process a method of context?

Because process is a method that then iterates on an array that lives in an object contained in the Scene (it's called World). And this World object that has all the System instances then calls the process of each system.

So I end up with a process method somewhere that needs the context to perform its duties and not the other way around. I don't know if this makes sense :slight_smile:

It might be interesting to create an example with somewhat closer structure to what your describe. I might have a sufficiently similar example that a shared solution would be possible.

But you can access both! You get data as an argument, and Scene is your self argument, so you have both.

You can see everything in my testbed project on GitHub.
The problem starts here: https://github.com/tanis2000/minigame-rust/blob/context-experiments/src/engine.rs#L585