Closed almindor closed 1 year ago
Hiya, sorry for the late reply, but in the snippet, I suspect dispatcher2
contains a system that is accessing State
mutably:
let mut state = world.fetch_mut::<State>(); // Get hold of a `Write<'_, State>`
state.do_something_else_mutably();
dispatcher2.dispatch(&world); // Trying to get another `Write'_, State>` causes the panic.
If you do this instead, it should work:
{
let mut state = world.fetch_mut::<State>(); // Get hold of a `Write<'_, State>`
state.do_something_else_mutably();
// drop the `Write<'_, State>`
}
dispatcher2.dispatch(&world); // Now you can get a `Write'_, State>` without panicking
A potential improvement is to allow multiple Write
's to be held, but only panic if both are dereferenced together. I haven't gotten familiar with the code to implement that yet.
A potential improvement is to allow multiple
Write
's to be held, but only panic if both are dereferenced together. I haven't gotten familiar with the code to implement that yet.
This would require being able to obtain a guard type from the Write
that then dereferences to the actual value. Then it could be made to panic if the guard already exists when another guard is created. (there is no way to leverage dereferencing alone to detect conflicts since we can't detect when the reference stops being used) (right now Write
basically acts as that guard so this would be adding another layer of types to defer the borrow that is currently held by Write
).
I think doing this by default everywhere would, introduce additional complexity from the user's view point of view in that they now need to call a method to get the guard instance before that will dereference into the inner type.
Potentially, we could introduce an optional LazyWrite<'a, T>
type that then has a method to get a Write<'a, T>
(i.e. Write
acts as the guard). However, I don't currently have any use case in mind where this would be useful and worth the complexity. If you have a compelling use case for such a feature, feel free to open an issue to discuss!
Description
I'm getting a runtime panic with:
Tried to fetch data, but it was already borrowed
Meta
Rust version: 1.39.0 Specs version / commit: 0.15.1 Operating system: Arch Linux
Reproduction
Steps to reproduce the behavior: Issue seems to only happen if I call dispatch on two different Dispatchers under specific conditions when in between I modify a resource inside a separate code block.
I suspect this forces the mut reference to that resource to be dropped but somehow the second dispatcher run panics with this error.
Execution quasi-example:
Expected behavior
Both dispatchers should've executed fine.
Backtrace