bevy/crates
Testare da83232fa8
Let Component::map_entities defer to MapEntities (#19414)
# Objective

The objective of this PR is to enable Components to use their
`MapEntities` implementation for `Component::map_entities`.

With the improvements to the entity mapping system, there is definitely
a huge reduction in boilerplate. However, especially since
`(Entity)HashMap<..>` doesn't implement `MapEntities` (I presume because
the lack of specialization in rust makes `HashMap<Entity|X, Entity|X>`
complicated), when somebody has types that contain these hashmaps they
can't use this approach.

More so, we can't even depend on the previous implementation, since
`Component::map_entities` is used instead of
`MapEntities::map_entities`. Outside of implementing `Component `and
`Component::map_entities` on these types directly, the only path forward
is to create a custom type to wrap the hashmaps and implement map
entities on that, or split these components into a wrapper type that
implement `Component`, and an inner type that implements `MapEntities`.

## Current Solution
The solution was to allow adding `#[component(map_entities)]` on the
component. By default this will defer to the `MapEntities`
implementation.

```rust
#[derive(Component)]
#[component(map_entities)]
struct Inventory {
    items: HashMap<Entity, usize>
}

impl MapEntities for Inventory {
    fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
        self.items = self.items
           .drain()
           .map(|(id, count)|(entity_mapper.get_mapped(id), count))
           .collect();
    }
}

```

You can use `#[component(map_entities = <function path>)]` instead to
substitute other code in for components. This function can also include
generics, but sso far I haven't been able to find a case where they are
needed.

```rust
#[derive(Component)]
#[component(map_entities = map_the_map)]
// Also works #[component(map_entities = map_the_map::<T,_>)]
struct Inventory<T> {
    items: HashMap<Entity, T>
}

fn map_the_map<T, M: EntityMapper>(inv: &mut Inventory<T>, entity_mapper: &mut M) {
    inv.items = inv.items
       .drain()
       .map(|(id, count)|(entity_mapper.get_mapped(id), count))
       .collect();
}

```

The idea is that with the previous changes to MapEntities, MapEntities
is implemented more for entity collections than for Components. If you
have a component that makes sense as both, `#[component(map_entities)]`
would work great, while otherwise a component can use
`#[component(map_entities = <function>)]` to change the behavior of
`Component::map_entities` without opening up the component type to be
included in other components.


## (Original Solution if you want to follow the PR)

The solution was to allow adding `#[component(entities)]` on the
component itself to defer to the `MapEntities` implementation

```rust
#[derive(Component)]
#[component(entities)]
struct Inventory {
    items: HashMap<Entity, usize>
}

impl MapEntities for Inventory {
    fn map_entities<M: EntityMapper>(&mut self, entity_mapper: &mut M) {
        self.items = self.items
           .drain()
           .map(|(id, count)|(entity_mapper.get_mapped(id), count))
           .collect();
    }
}

```

## Testing

I tested this by patching my local changes into my own bevy project. I
had a system that loads a scene file and executes some logic with a
Component that contains a `HashMap<Entity, UVec2>`, and it panics when
Entity is not found from another query. Since the 0.16 update this
system has reliably panicked upon attempting to the load the scene.

After patching my code in, I added `#[component(entities)]` to this
component, and I was able to successfully load the scene.

Additionally, I wrote a doc test.

## Call-outs
### Relationships
This overrules the default mapping of relationship fields. Anything else
seemed more problematic, as you'd have inconsistent behavior between
`MapEntities` and `Component`.
2025-06-23 21:05:04 +00:00
..
bevy_a11y Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_animation Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_anti_aliasing Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_app Rename num_entities to entity_count (#19781) 2025-06-23 05:08:02 +00:00
bevy_asset Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_audio Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_color Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_core_pipeline Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_core_widgets Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_derive Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_dev_tools Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_diagnostic Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_dylib Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_ecs Let Component::map_entities defer to MapEntities (#19414) 2025-06-23 21:05:04 +00:00
bevy_encase_derive Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_gilrs Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_gizmos Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_gltf Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_image Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_input Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_input_focus Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_internal Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_log Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_macro_utils Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_math Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_mesh Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_mikktspace Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_pbr Rename num_entities to entity_count (#19781) 2025-06-23 05:08:02 +00:00
bevy_picking Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_platform Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_ptr Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_reflect Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_remote Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_render bevy_solari: RIS for Direct Lighting (#19620) 2025-06-23 00:47:10 +00:00
bevy_scene Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_solari bevy_solari: RIS for Direct Lighting (#19620) 2025-06-23 00:47:10 +00:00
bevy_sprite Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_state Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_tasks Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_text Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_time Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_transform Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_ui Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_utils Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00
bevy_window Add newlines before impl blocks (#19746) 2025-06-22 23:07:02 +00:00
bevy_winit Bump Version after Release (#19774) 2025-06-22 23:06:43 +00:00