Hello,
I don't seek help in the sense that I am unable to make something compile, I just want to understand things better. Sadly I cannot replicate it on a playground code, it almost seems like it has to do with the borrow checker getting to it's limits the more nested generics get.
In a method of a generic type I want to call another &mut self
method at the end. But I can't do that because of this error:
error[E0502]: cannot borrow `self.log_manager` as mutable because it is also borrowed as immutable
--> src\reversible_system\state_manager.rs:683:9
|
643 | let current_entry = self.log_manager.try_entry().un...
| ---------------------------- immutable borrow occurs here
...
683 | self.log_manager.next().unwrap_or_panic(globals, &s...
| ^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs
here
684 | }
| - immutable borrow might be used here, when `current_state` is dropped and runs the destructor for type `<<T as ReversibleSystem>::State as StateOption>::StateFromLog<'_, <T as ReversibleSystem>::Chunk>`
The mentioned generic type at the end has basically no bounds, so it is not bounded to be Copy
.
I borrowed something from &self
earlier as the error noted, here:
let current_entry = self.log_manager.try_entry().unwrap_or_panic(globals, &self);
let current_state = get_state(¤t_entry.state_logged, states);
The code only compiles if I add drop(current_state)
before the last &mut self
call at the end:
drop(current_state);
self.log_manager.next().unwrap_or_panic(globals, &self); //this compiles now
}
It also works if I bound the generic type from the error message to implement Copy
, but I don't want it to be Copy
.
Now I am wondering, why does the compiler not drop the type where it can to make it runnable? Is it because I have to be explicit about the drop order? Is it confused from my generics? Something else?
I also have at some point to put logic in its own scope (brackets) as a partial move from a type happens there and I cannot drop
an incomplete type that would also run it's destructor at the method end.
I also would love if an explaination came with a code example why things happen the way they do.