Streaming iteration for non-IterQueryData#23218
Open
chescock wants to merge 5 commits intobevyengine:mainfrom
Open
Streaming iteration for non-IterQueryData#23218chescock wants to merge 5 commits intobevyengine:mainfrom
IterQueryData#23218chescock wants to merge 5 commits intobevyengine:mainfrom
Conversation
…ltiple items concurrently.
…multiple items concurrently.
…where necessary.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Objective
Follow-up to #21557
Support streaming iteration (a.k.a. lending iteration) for
QueryIterandQuerySortedIterso that it's possible to iterateQuery<Parent<&mut T>>.Double-check that all the
IterQueryDatabounds are correct and that the safety comments are correct. A few were not!Solution
Offer a
fetch_nextmethod onQueryIterandQuerySortedIterthat borrows the entire iterator to prevent aliasing, similar toQueryManyIter::QueryManyIter.Offer a
Query::iter_innermethod to produceQueryItereven when it is not anIterator. We cannot rely onIntoIteratorin that case, and Clippy objects to an inherent method calledinto_iter()that is not the standard trait method. This supersedes the existingiter_innermethod that only worked onReadOnlyQueryData.Add a missing
IterQueryDatabound toiter_combinations_mut. It yields multiple entities concurrently, so is never sound to use with non-IterQueryData. I think the reason I missed this originally is that it already has afetch_nextmethod and conditionalIteratorimpl. But evenfetch_nextyields multiple entities at once forQueryCombinationIter!Add a
ContiguousQueryData: IterQueryDatasupertrait bound.QueryContiguousIteralso yields multiple entities concurrently, so it does not make sense on non-iterable data. This was not actually unsound, though, asQueryContiguousIterdoes not callfetch.Finally, update some missing or incorrect safety comments.
Verify bounds on the other iterator types
To verify I didn't miss any bounds on iterator types, here is a list of every
Query*Itertype, and whether they support non-IterQueryData:Iter?CombinationIterContiguousIterIterManyIterManyUniqueIterParIterParManyIterParManyUniqueIterSortedIterSortedManyIterIterandSortedIterwere changed in this PR to support streaming iteration for allQueryData, while only implementingIteratorforIterQueryData.ManyIterandSortedManyIteralready had streaming iteration, and only implementIteratorwith a stricterReadOnlyQueryDatabound, since they may yield the same entity multiple times.ManyUniqueItercould theoretically be used with non-IterQueryData... but if you need to do streaming iteration anyway then you can calliter_many_mut()instead ofiter_many_unique_mut().CombinationIter,ContiguousIter, and thePar*Itertypes are all fundamentally about accessing multiple entities concurrently, and should always requireIterQueryData.CombinationIterandContiguousIterdid not haveIterQueryDatabounds, so fix that!Showcase
This is cheating a bit because we don't yet have
Parent<&mut T>, but once we do it would look like: