bevy/crates/bevy_ecs/src/query
Giacomo Stevanato e9e9e5e15d
Add query reborrowing (#14690)
# Objective

- Sometimes some method or function takes an owned `Query`, but we don't
want to give up ours;
- transmuting it technically a solution, but it more costly than
necessary.
- Make query iterators more flexible
- this would allow the equivalent of
`slice::split_first`/`slice::split_first_mut` for query iterators
  - helps with requests like #14685

## Solution

- Add a way for reborrowing queries, that is going from a `&'a mut
Query<'w, 's, D, F>` to a `Query<'a, 's, D, F>`:
- this is safe because the original query will be borrowed while the new
query exists and thus no aliased access can happen;
- it's basically the equivalent of going from `&'short mut &'long mut T`
to `&'short mut T` the the compiler automatically implements.
- Add a way for getting the remainder of a query iterator:
- this is interesting also because the original iterator keeps its
position, which was not possible before;
- this in turn requires a way to reborrow query fetches, which I had to
add to `WorldQuery`.

## Showcase

- You can now reborrow a `Query`, getting an equivalent `Query` with a
shorter lifetime. Previously this was possible for read-only queries by
using `Query::to_readonly`, now it's possible for mutable queries too;
- You can now separately iterate over the remainder of `QueryIter`.

## Migration Guide

- `WorldQuery` now has an additional `shrink_fetch` method you have to
implement if you were implementing `WorldQuery` manually.
2024-08-15 17:38:56 +00:00
..
access.rs Fix access conflicts for resources (#14635) 2024-08-06 14:35:41 +00:00
builder.rs Separate component and resource access (#14561) 2024-08-06 01:19:39 +00:00
error.rs Remove APIs deprecated in 0.13 (#11974) 2024-02-19 19:04:47 +00:00
fetch.rs Add query reborrowing (#14690) 2024-08-15 17:38:56 +00:00
filter.rs Add query reborrowing (#14690) 2024-08-15 17:38:56 +00:00
iter.rs Add query reborrowing (#14690) 2024-08-15 17:38:56 +00:00
mod.rs inline iter_combinations (#14680) 2024-08-09 17:44:37 +00:00
par_iter.rs multi_threaded feature rename (#12997) 2024-05-06 20:49:32 +00:00
state.rs Separate component and resource access (#14561) 2024-08-06 01:19:39 +00:00
world_query.rs Add query reborrowing (#14690) 2024-08-15 17:38:56 +00:00