-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Query::get_multiple and get_multiple_mut #3333
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
Closed
Closed
Changes from all commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
3e8288a
Method skeletons
alice-i-cecile 6e7600b
Preliminary implementation
alice-i-cecile 6b81770
Removed World API
alice-i-cecile ad9200c
Added doc tests
alice-i-cecile 3707686
Improved API design
alice-i-cecile 2a10fc2
Improved docs
alice-i-cecile 36df4e5
Remove PR noise
alice-i-cecile b7a1cbe
Typo fix
alice-i-cecile 499f804
Typo fix
alice-i-cecile 1a5895c
Minor docs fix
alice-i-cecile b13d970
Add get_pair and get_pair_mut
alice-i-cecile bfa1d84
Fix bugs in doc tests
alice-i-cecile d6173f1
Add `Query::from_state` helper method
alice-i-cecile 687b4fc
Better error-handling for get_pair
alice-i-cecile 04288ba
Allow arbitrary iterators for get_multiple_mut
alice-i-cecile c42bd70
Close code blocks so they compile...
alice-i-cecile 3bc5252
Fixed broken tests
alice-i-cecile 9cd5c23
Improve performance by returning an iter of Results
alice-i-cecile aa5a23b
Avoid needless allocation in get_multiple_mut
alice-i-cecile 5a4daf7
Fix inappropriate lifetime constraint on get_unchecked
alice-i-cecile 6ad1922
Allocate HashSet size correctly
alice-i-cecile d1e262a
Use unwrap_or
alice-i-cecile 183910c
Fix CI
alice-i-cecile d815de2
Initial conversion to const_generics
alice-i-cecile b2e499e
Fix off-by-one error
alice-i-cecile 1a7f1d3
Remove references to get_pair in docs
alice-i-cecile 69fb1c9
Fix doc links
alice-i-cecile 81d6a6c
Update docs
alice-i-cecile 94ca870
Update docs
alice-i-cecile a191c1e
Remove dead import
alice-i-cecile 724d6f3
Fix iterator
alice-i-cecile ccf903d
Remove QueryState -> Query helper method
alice-i-cecile 4e01623
Revert "Remove QueryState -> Query helper method"
alice-i-cecile c6e3241
Improve docs for `Query::from_state`
alice-i-cecile 189974f
Compile-fail test for Query::from_state
alice-i-cecile ab74fd3
Fix lifetimes per Boxy
alice-i-cecile 043abb1
Fix lifetimes per Boxy
alice-i-cecile 7cb4330
Iterator should require a mutable reference
alice-i-cecile b264ffc
Mostly fix liftime errors
alice-i-cecile be7ec0d
Fix remaining lifetime error
alice-i-cecile 1670877
Change return types
alice-i-cecile 10dd581
Record which entity caused QueryEntityError errors
alice-i-cecile 783b4f4
Remove Query::from_state method
alice-i-cecile a0d6632
Doc improvements
alice-i-cecile 9c74b6d
Move methods onto QueryState
alice-i-cecile aff9c65
Add infallible variants
alice-i-cecile de5bda7
i did not realise querystate was so unbelievably MESSED Up
BoxyUwU dfc8258
Progress!
alice-i-cecile 52d57c1
Return a result of an array, rather than an array of results
alice-i-cecile bee959e
More comments, more safety
alice-i-cecile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
crates/bevy_ecs_compile_fail_tests/tests/ui/query_state_uniqueness_safety.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use bevy_ecs::prelude::*; | ||
|
||
#[derive(Component, Eq, PartialEq, Debug)] | ||
struct A(usize); | ||
|
||
fn main() { | ||
let mut world = World::default(); | ||
world.spawn().insert(A(1)); | ||
|
||
let first_query_state: QueryState<&mut A> = world.query(); | ||
let second_query_state: QueryState<&mut A> = world.query(); | ||
|
||
let mut first_query = Query::from_state(&mut world, &first_query_state); | ||
// This should fail to compile, as another query is already active | ||
let mut second_query = Query::from_state(&mut world, &second_query_state); | ||
|
||
// This is a clear violation of no-aliased mutability | ||
assert_eq!(*first_query.single_mut(), *second_query.single_mut()); | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/bevy_ecs_compile_fail_tests/tests/ui/query_state_uniqueness_safety.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error[E0499]: cannot borrow `world` as mutable more than once at a time | ||
--> tests/ui/query_state_uniqueness_safety.rs:15:46 | ||
| | ||
13 | let mut first_query = Query::from_state(&mut world, &first_query_state); | ||
| ---------- first mutable borrow occurs here | ||
14 | // This should fail to compile, as another query is already active | ||
15 | let mut second_query = Query::from_state(&mut world, &second_query_state); | ||
| ^^^^^^^^^^ second mutable borrow occurs here | ||
... | ||
18 | assert_eq!(*first_query.single_mut(), *second_query.single_mut()); | ||
| ------------------------ first borrow later used here |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Imo this shouldn't use an iterator or hash set. To avoid allocations and hashing, it should do a brute force in place entity collision check. For small numbers of entities (which the const generic basically enforces) this will be much much faster. I expect 99% of use cases to use 2 or 3 entities (one comparison and three comparisons, respectively). And it should return a fixed size array:
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.
Quick and dirty impl that ignores error handling: