bevy/crates/bevy_ecs/compile_fail/tests/ui
Chris Russell 6f39e44c48
Introduce methods on QueryState to obtain a Query (#15858)
# Objective

Simplify and expand the API for `QueryState`.  

`QueryState` has a lot of methods that mirror those on `Query`. These
are then multiplied by variants that take `&World`, `&mut World`, and
`UnsafeWorldCell`. In addition, many of them have `_manual` variants
that take `&QueryState` and avoid calling `update_archetypes()`. Not all
of the combinations exist, however, so some operations are not possible.

## Solution

Introduce methods to get a `Query` from a `QueryState`. That will reduce
duplication between the types, and ensure that the full `Query` API is
always available for `QueryState`.

Introduce methods on `Query` that consume the query to return types with
the full `'w` lifetime. This avoids issues with borrowing where things
like `query_state.query(&world).get(entity)` don't work because they
borrow from the temporary `Query`.

Finally, implement `Copy` for read-only `Query`s. `get_inner` and
`iter_inner` currently take `&self`, so changing them to consume `self`
would be a breaking change. By making `Query: Copy`, they can consume a
copy of `self` and continue to work.

The consuming methods also let us simplify the implementation of methods
on `Query`, by doing `fn foo(&self) { self.as_readonly().foo_inner() }`
and `fn foo_mut(&mut self) { self.reborrow().foo_inner() }`. That
structure makes it more difficult to accidentally extend lifetimes,
since the safe `as_readonly()` and `reborrow()` 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 a `Cow`, then the consuming methods cannot be implemented, and
`Copy` cannot be implemented.

## Future Work

The next step is to mark the methods on `QueryState` as `#[deprecated]`,
and move the implementations into `Query`.

## Migration Guide

`Query::to_readonly` has been renamed to `Query::as_readonly`.
2025-02-05 18:33:15 +00:00
..
entity_ref_mut_lifetime_safety.rs
entity_ref_mut_lifetime_safety.stderr
query_exact_sized_iterator_safety.rs
query_exact_sized_iterator_safety.stderr
query_iter_combinations_mut_iterator_safety.rs
query_iter_combinations_mut_iterator_safety.stderr
query_iter_many_mut_iterator_safety.rs
query_iter_many_mut_iterator_safety.stderr
query_lifetime_safety.rs
query_lifetime_safety.stderr
query_to_readonly.rs Introduce methods on QueryState to obtain a Query (#15858) 2025-02-05 18:33:15 +00:00
query_to_readonly.stderr
query_transmute_safety.rs
query_transmute_safety.stderr
system_param_derive_readonly.rs
system_param_derive_readonly.stderr
system_query_get_lifetime_safety.rs
system_query_get_lifetime_safety.stderr
system_query_get_many_lifetime_safety.rs
system_query_get_many_lifetime_safety.stderr
system_query_get_many_mut_lifetime_safety.rs
system_query_get_many_mut_lifetime_safety.stderr
system_query_iter_lifetime_safety.rs
system_query_iter_lifetime_safety.stderr
system_query_iter_many_mut_lifetime_safety.rs
system_query_iter_many_mut_lifetime_safety.stderr
system_query_set_get_lifetime_safety.rs
system_query_set_get_lifetime_safety.stderr
system_query_set_iter_lifetime_safety.rs
system_query_set_iter_lifetime_safety.stderr
system_state_get_lifetime_safety.rs
system_state_get_lifetime_safety.stderr
system_state_iter_lifetime_safety.rs
system_state_iter_lifetime_safety.stderr
system_state_iter_mut_overlap_safety.rs
system_state_iter_mut_overlap_safety.stderr
world_query_derive.rs
world_query_derive.stderr