Hello all
I am not a rust developer, but I’m an intermediate C++ developer, and read a lot about rust. Looking for opportunity to use rust.
Like every C++ dev, I’m uncomfortable with Rust strict borrowing rules. (Ownership is awesome however in my opinion)
Specifically, I think that multi mutable borrowing should be legal and this is what we always did before rust. By multi mutable borrowing, I mean that an object could be referenced by many mutable references simultaneously. I know what Rust philosophy is, and I understood exactly why Rust avoid this - you can’t be sure that access is safe.
Two examples when it’s happen:
- Double linked list/tree. I know that the community has found a workaround without using unsafe, but it’s unnatural.
- Gaming/Systems Component relations - I don’t need to explain it to you very much, but I want to state an example: Amethyst Game Engine use entity component system(ECS), and by this design the engine gained the power to use mutable references simultaneously, because objects(here, entities), are referenced by integer index and not by pointer. This “cheating” is criticized in more detail at Jonathan Blow’s video.
Rust current solution is RefCel
, but this is just dynamic safety.
Why therefore low level devs use such design and feal safe? Because in their head/documentations, they claim: Reference A is using object X at this section of the “loop”, and reference B is using object X at other section of the “loop”.
I suggest (assuming it wasn’t suggested yet) a solution, how to put this “thinking” formally at rust: Add an access state concept.
Let “States” be a static user defined enum, and a object “component” which is linked to access state enum “States”.
Each borrowing of “component” should determine(manually or half-automatically?) which access state it wish to borrow.
If one wish to use the mutable/const reference, it must be in the scope of the access state. By access state scope I mean adding syntax/using macros to declare access scopes.
The compiler can easily validate this, and the user gained a more detailed access state transparency.
I don’t know if it might solves double linked list/tree issue somehow, but I’m sure it solves components relations. For example, in game logic you have steps in the game loop - they correspond to access states.
What do you think about this? Any similar idea has been suggested?