bevy/crates
Gino Valente 51accd34ed
bevy_reflect: Add dynamic type data access and iteration to TypeRegistration (#15347)
# Objective

There's currently no way to iterate through all the type data in a
`TypeRegistration`. While these are all type-erased, it can still be
useful to see what types (by `TypeId`) are registered for a given type.

Additionally, it might be good to have ways of dynamically working with
`TypeRegistration`.

## Solution

Added a way to iterate through all type data on a given
`TypeRegistration`. This PR also adds methods for working with type data
dynamically as well as methods for conveniently checking if a given type
data exists on the registration.

I also took this opportunity to reorganize the methods on
`TypeRegistration` as it has always bothered me haha (i.e. the
constructor not being at the top, etc.).

## Testing

You can test locally by running:

```
cargo test --package bevy_reflect
```

---

## Showcase

The type-erased type data on a `TypeRegistration` can now be iterated!

```rust
#[derive(Reflect)]
struct Foo;

#[derive(Clone)]
struct DataA(i32);

#[derive(Clone)]
struct DataB(i32);

let mut registration = TypeRegistration::of::<Foo>();
registration.insert(DataA(123));
registration.insert(DataB(456));

let mut iter = registration.iter();

let (id, data) = iter.next().unwrap();
assert_eq!(id, TypeId::of::<DataA>());
assert_eq!(data.downcast_ref::<DataA>().unwrap().0, 123);

let (id, data) = iter.next().unwrap();
assert_eq!(id, TypeId::of::<DataB>());
assert_eq!(data.downcast_ref::<DataB>().unwrap().0, 456);

assert!(iter.next().is_none());
```
2024-09-23 17:21:22 +00:00
..
bevy_a11y Reflected traits for resources and components: bevy_a11y (#15192) 2024-09-14 01:43:16 +00:00
bevy_animation Allow animation clips to animate arbitrary properties. (#15282) 2024-09-23 17:14:12 +00:00
bevy_app Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_asset bevy_reflect: Add ReflectRef/ReflectMut/ReflectOwned convenience casting methods (#15235) 2024-09-23 16:50:46 +00:00
bevy_audio Reflect derived traits on all components and resources: bevy_audio (#15211) 2024-09-15 14:24:00 +00:00
bevy_color Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
bevy_core Unify crate-level preludes (#15080) 2024-09-08 17:10:57 +00:00
bevy_core_pipeline Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_derive Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_dev_tools Added HeadlessPlugins (#15203) (#15260) 2024-09-19 16:44:43 +00:00
bevy_diagnostic Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_dylib Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_ecs Allow animation clips to animate arbitrary properties. (#15282) 2024-09-23 17:14:12 +00:00
bevy_encase_derive Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_gilrs Update gilrs requirement from 0.10.1 to 0.11.0 (#15245) 2024-09-16 23:34:04 +00:00
bevy_gizmos Reduce runtime panics through SystemParam validation (#15276) 2024-09-23 16:54:21 +00:00
bevy_gltf Allow animation clips to animate arbitrary properties. (#15282) 2024-09-23 17:14:12 +00:00
bevy_hierarchy move ShortName to bevy_reflect (#15340) 2024-09-21 20:52:46 +00:00
bevy_input Use of deprecated function in example for ButtonInput (#15221) 2024-09-15 15:22:39 +00:00
bevy_internal Spirv passthrough main (adopted, part deux) (#15352) 2024-09-22 14:51:14 +00:00
bevy_log Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_macro_utils Generate links to definition in source code pages on docs.rs and dev-docs.bevyengine.org (#12965) 2024-07-29 23:10:16 +00:00
bevy_math Fix floating point math (#15239) 2024-09-16 23:28:12 +00:00
bevy_mikktspace Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_pbr Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_picking Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_ptr Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_reflect bevy_reflect: Add dynamic type data access and iteration to TypeRegistration (#15347) 2024-09-23 17:21:22 +00:00
bevy_render Reduce runtime panics through SystemParam validation (#15276) 2024-09-23 16:54:21 +00:00
bevy_scene Rename push children to add children (#15196) 2024-09-16 23:16:04 +00:00
bevy_sprite feature gate picking backends (#15369) 2024-09-22 19:35:15 +00:00
bevy_state Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_tasks Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_text Reflect derived traits on all components and resources: bevy_text (#15229) 2024-09-15 17:21:02 +00:00
bevy_time Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_transform Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_ui UI Scrolling (#15291) 2024-09-23 17:17:58 +00:00
bevy_utils move ShortName to bevy_reflect (#15340) 2024-09-21 20:52:46 +00:00
bevy_window Allow to expect (adopted) (#15301) 2024-09-20 19:16:42 +00:00
bevy_winit Remove ReceivedCharacter (#15126) 2024-09-10 00:22:06 +00:00