Greetings ! I have a need to traverse a tree structure with N-Child with struct Actor as nodes and struct Transmission as connections.
The requirement is a helper function which takes the root node and a closure called for each node and exposes the current node and the incoming Transmission object as args of the closure.
The previous implementation without mutability was working fine until there was a need for mutations inside the closure.
Is there any work around to achieve the desired needs ?
error[E0499]: cannot borrow `*recip` as mutable more than once at a time
--> src\documents\flows.rs:381:52
|
381 | self.iterator( &mut recip.actor, Some( recip ) );
| -------- ---------------- ^^^^^ second mutable borrow occurs here
| | |
| | first mutable borrow occurs here
| first borrow later used by call
Another requirement was added which is accessing parent Transmission object (read only) and again the borrow checker was a road block to achieve it.
I had to use the runtime checking to bypass the borrow checker using Ref & RefCell. I also changed the tree structure to begin with Transmission struct instead of Actor.
This implementation work perfectly but does have some down sides:
Performance hit
Juggling with .borrow(), .borrow_mut() and scopes inside the callback to avoid runtime panics