Repository navigation
Change QueryManyIter to return Result instead of only matched items
#25200
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
Merged
alice-i-cecile
merged 6 commits into
bevyengine:main
from
JonasJebing:query-iter-many-results
Aug 5, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f593b61
Change `QueryManyIter` to return `Result` instead of only matched items
JonasJebing e73c920
add `query_many_iter_results.md` migration guide
JonasJebing 8f1c672
delete leftover comments
JonasJebing df3406d
fix markdown lints
JonasJebing d77d369
change order of rustdoc examples on `Query::iter_many`
JonasJebing a96f537
Merge branch 'main' into query-iter-many-results
JonasJebing 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
121 changes: 121 additions & 0 deletions
121
_release-content/migration-guides/query_many_iter_results.md
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,121 @@ | ||
| --- | ||
| title: Query iterators from "many" entities now iterate over `Result` instead of `QueryData::Item`. | ||
| pull_requests: [25200] | ||
| --- | ||
|
|
||
| The following iterators now return `Result<QueryData::Item<'w, 's>, QueryEntityError>` | ||
| instead of `QueryData::Item<'w, 's>` as the `Iterator::Item`. | ||
|
|
||
| - `QueryManyIter` | ||
| - `QueryManyUniqueIter` | ||
| - `QuerySortedManyIter` | ||
|
|
||
| These iterators are created by the following methods. | ||
|
|
||
| - `Query::iter_many` | ||
| - `Query::iter_many_mut` | ||
| - `Query::iter_many_unique` | ||
| - `Query::iter_many_unique_mut` | ||
| - `QueryManyIter::sort*` | ||
|
|
||
| Likewise, the following parallel iterator methods now have `Result<QueryData::Item<'w, 's>, QueryEntityError>` | ||
| instead of `QueryData::Item<'w, 's>` as the closure argument. | ||
|
|
||
| - `QueryParManyIter::for_each` | ||
| - `QueryParManyIter::for_each_init`, | ||
| - `QueryParManyUniqueIter::for_each` | ||
| - `QueryParManyUniqueIter::for_each_init`, | ||
|
|
||
| These parallel iterators are created by the following methods. | ||
|
|
||
| - `Query::par_iter_many` | ||
| - `Query::par_iter_many_unique` | ||
| - `Query::par_iter_many_unique_mut` | ||
|
|
||
| These changes were made to allow for full flexibility in handling not matched or not spawned entities. | ||
| This allows easier debugging by making these errors visible through a panic or logging | ||
| instead of only giving the option to silently ignore these errors. | ||
|
|
||
| For `QueryManyIter` and `QueryManyUniqueIter` users can migrate using | ||
|
|
||
| - `QueryManyIter::matched` | ||
| - `QueryManyUniqueIter::matched` | ||
|
|
||
| which provide the same behavior as before. | ||
|
|
||
| ```rust | ||
| // 0.19 | ||
| fn my_system(entity_list: Res<MyEntityList>, my_component_query: Query<&MyComponent>) { | ||
| for my_component in my_component_query.iter_many(entity_list.iter()) { | ||
| // ... | ||
| } | ||
| } | ||
|
|
||
| // 0.20 | ||
| fn my_system(entity_list: Res<MyEntityList>, my_component_query: Query<&MyComponent>) { | ||
| for my_component in my_component_query.iter_many(entity_list.iter()).matched() { | ||
| // ... | ||
| } | ||
| } | ||
|
|
||
| // 0.19 | ||
| fn my_mutation_system(entity_list: Res<MyEntityList>, mut my_component_query: Query<&mut MyComponent>) { | ||
| let mut iter = my_component_query.iter_many_mut(entity_list.iter()); | ||
| while let Some(my_component) in iter.fetch_next() { | ||
| // ... | ||
| } | ||
| } | ||
|
|
||
| // 0.20 | ||
| fn my_mutation_system(entity_list: Res<MyEntityList>, mut my_component_query: Query<&mut MyComponent>) { | ||
| let mut iter = my_component_query.iter_many_mut(entity_list.iter()).matched(); | ||
| while let Some(my_component) in iter.fetch_next() { | ||
| // ... | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| For `QuerySortedManyIter` you can only use `.flat_map(Result::ok)`. | ||
|
|
||
| ```rust | ||
| // 0.19 | ||
| fn my_system(entity_list: Res<MyEntityList>, my_component_query: Query<&MyComponent>) { | ||
| for my_component in my_component_query.iter_many(entity_list.iter()) | ||
| .sort::<&MyComponent>() | ||
| { | ||
| // ... | ||
| } | ||
| } | ||
|
|
||
| // 0.20 | ||
| fn my_system(entity_list: Res<MyEntityList>, my_component_query: Query<&MyComponent>) { | ||
| for my_component in my_component_query.iter_many(entity_list.iter()) | ||
| .sort::<&MyComponent>() | ||
| .flat_map(Result::ok) | ||
| { | ||
| // ... | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| For `QueryParManyIter` and `QueryParManyUniqueIter` you need to return early when there is an error | ||
| to get the same `matched` behavior as before. | ||
|
|
||
| ```rust | ||
| // 0.19 | ||
| fn my_system(entity_list: Res<MyEntityList>, my_component_query: Query<&MyComponent>) { | ||
| my_component_query.par_iter_many(entity_list.iter()).for_each(|my_component| { | ||
| // ... | ||
| }); | ||
| } | ||
|
|
||
| // 0.20 | ||
| fn my_system(entity_list: Res<MyEntityList>, my_component_query: Query<&MyComponent>) { | ||
| my_component_query.par_iter_many(entity_list.iter()).for_each(|my_component| { | ||
| let Ok(my_component) = my_component else { | ||
| return; | ||
| }; | ||
| // ... | ||
| }); | ||
| } | ||
| ``` | ||
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
Oops, something went wrong.
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.
random, and not sure if this is even possible but i wonder if a
filter_mapcould work with the iterator chains better than thelet Ok(..) else { }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.
Since
QueryParManyIterdoes not actually implement theIteratortrait, it is not possible to callIterator::filter_map.