bevy/crates
Zachary Harrold 3bfc427666
Add mappings to EntityMapper (#13727)
# Objective

- Fixes #13703

## Solution

- Added `mappings` to the `EntityMapper` trait, which returns an
iterator over currently tracked `Entity` to `Entity` mappings.
- Added `DynEntityMapper` as an [object
safe](https://doc.rust-lang.org/reference/items/traits.html#object-safety)
alternative to `EntityMapper`.
- Added `assert_object_safe` as a helper for ensuring traits are object
safe.

## Testing

- Added new unit test `entity_mapper_iteration` which tests the
`SceneEntityMapper` implementation of `EntityMapper::mappings`.
- Added unit tests to ensure `DynEntityMapper`, `DynEq` and `DynHash`
are object safe.
- Passed CI on my Windows 10 development environment

---

## Changelog

- Added `mappings` to `EntityMapper` trait.

## Migration Guide

- If you are implementing `EntityMapper` yourself, you can use the below
as a stub implementation:

```rust
fn mappings(&self) -> impl Iterator<Item = (Entity, Entity)> {
    unimplemented!()
}
```

- If you were using `EntityMapper` as a trait object (`dyn
EntityMapper`), instead use `dyn DynEntityMapper` and its associated
methods.

## Notes

- The original issue proposed returning a `Vec` from `EntityMapper`
instead of an `impl Iterator` to preserve its object safety. This is a
simpler option, but also forces an allocation where it isn't strictly
needed. I've opted for this split into `DynEntityMapper` and
`EntityMapper` as it's been done several times across Bevy already, and
provides maximum flexibility to users.
- `assert_object_safe` is an empty function, since the assertion
actually happens once you try to use a `dyn T` for some trait `T`. I
have still added this function to clearly document what object safety is
within Bevy, and to create a standard way to communicate that a given
trait must be object safe.
- Other traits should have tests added to ensure object safety, but I've
left those off to avoid cluttering this PR further.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
2024-06-08 12:52:23 +00:00
..
bevy_a11y fix: upgrade to winit v0.30 (#13366) 2024-06-03 13:06:48 +00:00
bevy_animation Make the component types of the new animation players clonable. (#13736) 2024-06-07 21:51:24 +00:00
bevy_app Forward exit codes in default app runner (#13674) 2024-06-04 21:40:40 +00:00
bevy_asset Provide more information when a filewatcher failure is hit. (#13715) 2024-06-06 19:20:07 +00:00
bevy_audio Update to rodio 0.18 (#13612) 2024-06-02 14:25:51 +00:00
bevy_color Added a Grey trait, and implementations on baked-in colors. Fixes #13206 (#13237) 2024-05-26 12:53:50 +00:00
bevy_core Determine msrv for every standalone bevy_* crate. (#13211) 2024-05-13 18:26:41 +00:00
bevy_core_pipeline Allow mix of hdr and non-hdr cameras to same render target (#13419) 2024-06-06 20:55:05 +00:00
bevy_derive Deprecate dynamic plugins (#13080) 2024-05-20 20:01:28 +00:00
bevy_dev_tools Move utilities from examples to bevy_state and add concept of state-scoped entities (#13649) 2024-06-04 11:44:34 +00:00
bevy_diagnostic Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_dylib Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_dynamic_plugin Deprecate dynamic plugins (#13080) 2024-05-20 20:01:28 +00:00
bevy_ecs Add mappings to EntityMapper (#13727) 2024-06-08 12:52:23 +00:00
bevy_encase_derive Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_gilrs Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_gizmos Rename Rotation2d to Rot2 (#13694) 2024-06-05 21:51:13 +00:00
bevy_gltf Add labels to Gltf Node and Mesh assets (#13558) 2024-06-05 23:10:33 +00:00
bevy_hierarchy Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_input flush key_input cache when Bevy loses focus (Adopted) (#13678) 2024-06-05 02:06:47 +00:00
bevy_internal Move state installation methods from bevy_app to bevy_state (#13637) 2024-06-03 13:47:08 +00:00
bevy_log Improve tracing layer customization (#13159) 2024-05-12 21:16:56 +00:00
bevy_macro_utils Add README.md to all crates (#13184) 2024-05-02 18:56:00 +00:00
bevy_math Rename Rotation2d to Rot2 (#13694) 2024-06-05 21:51:13 +00:00
bevy_mikktspace Determine msrv for every standalone bevy_* crate. (#13211) 2024-05-13 18:26:41 +00:00
bevy_pbr Use TBN in apply_normal_mapping in pbr_prepass (#13716) 2024-06-06 19:04:30 +00:00
bevy_ptr add Debug for ptr types (#13498) 2024-05-24 21:25:11 +00:00
bevy_reflect improved the error message by insert_boxed (issue #13646) (again) (#13706) 2024-06-07 20:56:16 +00:00
bevy_render Allow mix of hdr and non-hdr cameras to same render target (#13419) 2024-06-06 20:55:05 +00:00
bevy_scene Map entities from a resource when written to the world. (#13650) 2024-06-03 16:33:24 +00:00
bevy_sprite Uncouple DynamicTextureAtlasBuilder from assets (#13717) 2024-06-08 12:38:03 +00:00
bevy_state rename the crate bevy_state_macros_official back to its original name (#13732) 2024-06-07 12:49:21 +00:00
bevy_tasks fix: upgrade to winit v0.30 (#13366) 2024-06-03 13:06:48 +00:00
bevy_text Uncouple DynamicTextureAtlasBuilder from assets (#13717) 2024-06-08 12:38:03 +00:00
bevy_time Adds doc note that Timer and Stopwatch must be progressed manually (#13441) 2024-05-20 19:46:25 +00:00
bevy_transform Fix links to Transform in Transform and GlobalTransform docs (#13704) 2024-06-06 20:06:54 +00:00
bevy_ui make UI text rendering camera driven (#13697) 2024-06-06 00:20:50 +00:00
bevy_utils Add mappings to EntityMapper (#13727) 2024-06-08 12:52:23 +00:00
bevy_window rename touchpad to gesture, and add new gestures (#13660) 2024-06-04 12:44:25 +00:00
bevy_winit make sure windows are dropped on main thread (#13686) 2024-06-05 18:13:59 +00:00