Introduce methods on QueryState to obtain a Query #15858
+540
−277
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
Simplify and expand the API for
QueryState
.QueryState
has a lot of methods that mirror those onQuery
. These are then multiplied by variants that take&World
,&mut World
, andUnsafeWorldCell
. In addition, many of them have_manual
variants that take&QueryState
and avoid callingupdate_archetypes()
. Not all of the combinations exist, however, so some operations are not possible.Solution
Introduce methods to get a
Query
from aQueryState
. That will reduce duplication between the types, and ensure that the fullQuery
API is always available forQueryState
.Introduce methods on
Query
that consume the query to return types with the full'w
lifetime. This avoids issues with borrowing where things likequery_state.query(&world).get(entity)
don't work because they borrow from the temporaryQuery
.Finally, implement
Copy
for read-onlyQuery
s.get_inner
anditer_inner
currently take&self
, so changing them to consumeself
would be a breaking change. By makingQuery: Copy
, they can consume a copy ofself
and continue to work.The consuming methods also let us simplify the implementation of methods on
Query
, by doingfn foo(&self) { self.as_readonly().foo_inner() }
andfn foo_mut(&mut self) { self.reborrow().foo_inner() }
. That structure makes it more difficult to accidentally extend lifetimes, since the safeas_readonly()
andreborrow()
methods shrink them appropriately. The optimizer is able to see that they are both identity functions and inline them, so there should be no performance cost.Note that this change would conflict with #15848. If
QueryState
is stored as aCow
, then the consuming methods cannot be implemented, andCopy
cannot be implemented.Future Work
The next step is to mark the methods on
QueryState
as#[deprecated]
, and move the implementations intoQuery
.Migration Guide
Query::to_readonly
has been renamed toQuery::as_readonly
.