-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query::get_multiple and get_multiple_mut #3333
Conversation
fdccab5
to
1b6e83d
Compare
c65edc6
to
8b448cb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Community review! Looks good to me, but strained myself to get at least something to comment 😉
Also, probably just stating the obvious here, but adding an error to |
We have accepted that every release will be extremely breaking, at least for now ;) I'm reasonably happy with causing compiler errors to draw attention to new behavior in this case. |
For the lack of variadics, what about using and returning |
Getting multiple EntityMut is fundamentally impossible due to the ability to add and remove components. Fixing this is out of scope for this PR.
crates/bevy_ecs/src/system/query.rs
Outdated
/// assert_eq!(*a_iterator.next().unwrap(), A(1)); | ||
/// ``` | ||
#[inline] | ||
pub fn get_multiple<const N: usize>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these should be implemented on QueryState with appropriate variants, then wired up to Query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep... I don't love this whole API duplication, but I think that's a reasonable call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is necessary if we want "self driving" queries with direct world access (and I think we do).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add a compile fail test making sure that you cant make a GetMultipleMut
iterator and then call methods on Query
and then use the results from GetMultipleMut
to get an aliased mutable ref
I don't really think that we should be adding let mut world = World::default();
let mut system_state = SystemState::<Query<&mut Foo>>::new(&mut world);
let mut query = system_state.get_mut(&mut world); |
Co-authored-by: Boxy <supbscripter@gmail.com>
Co-authored-by: Boxy <supbscripter@gmail.com>
Co-authored-by: Boxy <supbscripter@gmail.com>
Lol those errors are not super helpful... can be fixed by replacing |
Full credit to Boxy
Closing in favor of the less messy #4298. |
…4298) # Objective - The inability to have multiple active mutable borrows into a query is a common source of borrow-checker pain for users. - This is a pointless restriction if and only if we can guarantee that the entities they are accessing are unique. - This could already by bypassed with get_unchecked, but that is an extremely unsafe API. - Closes #2042. ## Solution - Add `get_multiple`, `get_multiple_mut` and their unchecked equivalents (`multiple` and `multiple_mut`) to `Query` and `QueryState`. - Improve the `QueryEntityError` type to provide more useful error information. ## Changelog - Added `get_multiple`, `get_multiple_mut` and their unchecked equivalents (`multiple` and `multiple_mut`) to Query and QueryState. ## Migration Guide - The `QueryEntityError` enum now has a `AliasedMutability variant, and returns the offending entity. ## Context This is a fresh attempt at #3333; rebasing was behaving very badly and it was important to rebase on top of the recent query soundness fixes. Many thanks to all the reviewers in that thread, especially @BoxyUwU for the help with lifetimes. ## To-do - [x] Add compile fail tests - [x] Successfully deduplicate code - [x] Decide what to do about failing doc tests - [x] Get some reviews for lifetime soundness
…evyengine#4298) # Objective - The inability to have multiple active mutable borrows into a query is a common source of borrow-checker pain for users. - This is a pointless restriction if and only if we can guarantee that the entities they are accessing are unique. - This could already by bypassed with get_unchecked, but that is an extremely unsafe API. - Closes bevyengine#2042. ## Solution - Add `get_multiple`, `get_multiple_mut` and their unchecked equivalents (`multiple` and `multiple_mut`) to `Query` and `QueryState`. - Improve the `QueryEntityError` type to provide more useful error information. ## Changelog - Added `get_multiple`, `get_multiple_mut` and their unchecked equivalents (`multiple` and `multiple_mut`) to Query and QueryState. ## Migration Guide - The `QueryEntityError` enum now has a `AliasedMutability variant, and returns the offending entity. ## Context This is a fresh attempt at bevyengine#3333; rebasing was behaving very badly and it was important to rebase on top of the recent query soundness fixes. Many thanks to all the reviewers in that thread, especially @BoxyUwU for the help with lifetimes. ## To-do - [x] Add compile fail tests - [x] Successfully deduplicate code - [x] Decide what to do about failing doc tests - [x] Get some reviews for lifetime soundness
…evyengine#4298) # Objective - The inability to have multiple active mutable borrows into a query is a common source of borrow-checker pain for users. - This is a pointless restriction if and only if we can guarantee that the entities they are accessing are unique. - This could already by bypassed with get_unchecked, but that is an extremely unsafe API. - Closes bevyengine#2042. ## Solution - Add `get_multiple`, `get_multiple_mut` and their unchecked equivalents (`multiple` and `multiple_mut`) to `Query` and `QueryState`. - Improve the `QueryEntityError` type to provide more useful error information. ## Changelog - Added `get_multiple`, `get_multiple_mut` and their unchecked equivalents (`multiple` and `multiple_mut`) to Query and QueryState. ## Migration Guide - The `QueryEntityError` enum now has a `AliasedMutability variant, and returns the offending entity. ## Context This is a fresh attempt at bevyengine#3333; rebasing was behaving very badly and it was important to rebase on top of the recent query soundness fixes. Many thanks to all the reviewers in that thread, especially @BoxyUwU for the help with lifetimes. ## To-do - [x] Add compile fail tests - [x] Successfully deduplicate code - [x] Decide what to do about failing doc tests - [x] Get some reviews for lifetime soundness
Objective
get_unchecked
, but that is an extremely unsafe API.Solution
Query::get_multiple
andget_multiple_mut
, to provide access to the data of more than one entity at once.get_multiple_mut
enforces uniqueness (to ensure soundness), butget_multiple
does not (to reduce overhead).World
, as multipleEntityMut
cannot exist at once: the ability to add and remove components to entities with it make that design fundamentally unsound.Query
and then use these methods.Vec
or other collection in order to avoid allocations and ensure maximal flexibility.Breaking
Technically, this is a breaking change, as I've added a
AliasedMutability
variant to theQueryEntityError
enum.