bevy/crates
ira 92ddfe8ad4 Add methods for querying lists of entities. (#4879)
# Objective
Improve querying ergonomics around collections and iterators of entities.

Example how queries over Children might be done currently. 
```rust
fn system(foo_query: Query<(&Foo, &Children)>, bar_query: Query<(&Bar, &Children)>) {
    for (foo, children) in &foo_query {
        for child in children.iter() {
            if let Ok((bar, children)) = bar_query.get(*child) {
                for child in children.iter() {
                    if let Ok((foo, children)) = foo_query.get(*child) {
                        // D:
                    }
                }
            }
        }
    }
}
```
Answers #4868
Partially addresses #4864
Fixes #1470
## Solution
Based on the great work by @deontologician in #2563 

Added `iter_many` and `many_for_each_mut` to `Query`.
These take a list of entities (Anything that implements `IntoIterator<Item: Borrow<Entity>>`).

`iter_many` returns a `QueryManyIter` iterator over immutable results of a query (mutable data will be cast to an immutable form).

`many_for_each_mut` calls a closure for every result of the query, ensuring not aliased mutability. 
This iterator goes over the list of entities in order and returns the result from the query for it. Skipping over any entities that don't match the query.

Also added `unsafe fn iter_many_unsafe`.

### Examples
```rust
#[derive(Component)]
struct Counter {
    value: i32
}

#[derive(Component)]
struct Friends {
    list: Vec<Entity>,
}

fn system(
    friends_query: Query<&Friends>,
    mut counter_query: Query<&mut Counter>,
) {
    for friends in &friends_query {
        for counter in counter_query.iter_many(&friends.list) {
            println!("Friend's counter: {:?}", counter.value);
        }
        
        counter_query.many_for_each_mut(&friends.list, |mut counter| {
            counter.value += 1;
            println!("Friend's counter: {:?}", counter.value);
        });
    }
}

```

Here's how example in the Objective section can be written with this PR.
```rust
fn system(foo_query: Query<(&Foo, &Children)>, bar_query: Query<(&Bar, &Children)>) {
    for (foo, children) in &foo_query {
        for (bar, children) in bar_query.iter_many(children) {
            for (foo, children) in foo_query.iter_many(children) {
                // :D
            }
        }
    }
}
```
## Additional changes
Implemented `IntoIterator` for `&Children` because why not.
## Todo
- Bikeshed!

Co-authored-by: deontologician <deontologician@gmail.com>

Co-authored-by: devil-ira <justthecooldude@gmail.com>
2022-06-06 16:09:16 +00:00
..
bevy_animation Clippy improvements (#4665) 2022-05-31 01:38:07 +00:00
bevy_app Clippy improvements (#4665) 2022-05-31 01:38:07 +00:00
bevy_asset Revert ndk-glue to 0.5 to synchronize with winit (#4916) 2022-06-04 14:30:44 +00:00
bevy_audio Clippy improvements (#4665) 2022-05-31 01:38:07 +00:00
bevy_core Move primitive type registration into bevy_reflect (#4844) 2022-06-03 20:28:44 +00:00
bevy_core_pipeline Camera Driven Viewports (#4898) 2022-06-05 00:27:49 +00:00
bevy_derive Decouple some dependencies (#3886) 2022-04-27 19:08:11 +00:00
bevy_diagnostic Split time functionality into bevy_time (#4187) 2022-05-26 00:27:18 +00:00
bevy_dylib Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_dynamic_plugin Bump Bevy to 0.8.0-dev (#4505) 2022-04-17 23:04:52 +00:00
bevy_ecs Add methods for querying lists of entities. (#4879) 2022-06-06 16:09:16 +00:00
bevy_ecs_compile_fail_tests Add methods for querying lists of entities. (#4879) 2022-06-06 16:09:16 +00:00
bevy_encase_derive Migrate to encase from crevice (#4339) 2022-05-18 21:09:21 +00:00
bevy_gilrs Update gilrs to v0.9 (#4848) 2022-05-30 17:26:23 +00:00
bevy_gltf Camera Driven Rendering (#4745) 2022-06-02 00:12:17 +00:00
bevy_hierarchy Add methods for querying lists of entities. (#4879) 2022-06-06 16:09:16 +00:00
bevy_input Update keyboard.rs docs in bevy_input (#4517) 2022-05-17 04:16:54 +00:00
bevy_internal Revert ndk-glue to 0.5 to synchronize with winit (#4916) 2022-06-04 14:30:44 +00:00
bevy_log Update tracing-tracy requirement from 0.8.0 to 0.9.0 (#4786) 2022-05-27 11:54:57 +00:00
bevy_macro_utils bevy_reflect_derive: Tidying up the code (#4712) 2022-05-12 19:43:23 +00:00
bevy_math Document bevy_math (#4591) 2022-04-26 18:23:29 +00:00
bevy_mikktspace Generate vertex tangents using mikktspace (#3872) 2022-05-31 22:53:54 +00:00
bevy_pbr Camera Driven Viewports (#4898) 2022-06-05 00:27:49 +00:00
bevy_ptr Clippy improvements (#4665) 2022-05-31 01:38:07 +00:00
bevy_reflect Move primitive type registration into bevy_reflect (#4844) 2022-06-03 20:28:44 +00:00
bevy_render diagnostics: meaningful error when graph node has wrong number of inputs (#4924) 2022-06-06 15:47:52 +00:00
bevy_scene Update gilrs to v0.9 (#4848) 2022-05-30 17:26:23 +00:00
bevy_sprite Camera Driven Rendering (#4745) 2022-06-02 00:12:17 +00:00
bevy_tasks Remove unused CountdownEvent (#4290) 2022-04-26 21:20:12 +00:00
bevy_text Camera Driven Rendering (#4745) 2022-06-02 00:12:17 +00:00
bevy_time Split time functionality into bevy_time (#4187) 2022-05-26 00:27:18 +00:00
bevy_transform Nightly clippy fixes (#3491) 2022-05-17 04:38:03 +00:00
bevy_ui Camera Driven Viewports (#4898) 2022-06-05 00:27:49 +00:00
bevy_utils Update gilrs to v0.9 (#4848) 2022-05-30 17:26:23 +00:00
bevy_window Camera Driven Rendering (#4745) 2022-06-02 00:12:17 +00:00
bevy_winit Fix crash when using Duration::MAX (#4900) 2022-06-02 19:42:20 +00:00