bevy/crates
Gino Valente aa241672e1
bevy_reflect: Nested TypeInfo getters (#13321)
# Objective

Right now, `TypeInfo` can be accessed directly from a type using either
`Typed::type_info` or `Reflect::get_represented_type_info`.

However, once that `TypeInfo` is accessed, any nested types must be
accessed via the `TypeRegistry`.

```rust
#[derive(Reflect)]
struct Foo {
  bar: usize
}

let registry = TypeRegistry::default();

let TypeInfo::Struct(type_info) = Foo::type_info() else {
  panic!("expected struct info");
};

let field = type_info.field("bar").unwrap();

let field_info = registry.get_type_info(field.type_id()).unwrap();
assert!(field_info.is::<usize>());;
```

## Solution

Enable nested types within a `TypeInfo` to be retrieved directly.

```rust
#[derive(Reflect)]
struct Foo {
  bar: usize
}

let TypeInfo::Struct(type_info) = Foo::type_info() else {
  panic!("expected struct info");
};

let field = type_info.field("bar").unwrap();

let field_info = field.type_info().unwrap();
assert!(field_info.is::<usize>());;
```

The particular implementation was chosen for two reasons.

Firstly, we can't just store `TypeInfo` inside another `TypeInfo`
directly. This is because some types are recursive and would result in a
deadlock when trying to create the `TypeInfo` (i.e. it has to create the
`TypeInfo` before it can use it, but it also needs the `TypeInfo` before
it can create it). Therefore, we must instead store the function so it
can be retrieved lazily.

I had considered also using a `OnceLock` or something to lazily cache
the info, but I figured we can look into optimizations later. The API
should remain the same with or without the `OnceLock`.

Secondly, a new wrapper trait had to be introduced: `MaybeTyped`. Like
`RegisterForReflection`, this trait is `#[doc(hidden)]` and only exists
so that we can properly handle dynamic type fields without requiring
them to implement `Typed`. We don't want dynamic types to implement
`Typed` due to the fact that it would make the return type
`Option<&'static TypeInfo>` for all types even though only the dynamic
types ever need to return `None` (see #6971 for details).

Users should never have to interact with this trait as it has a blanket
impl for all `Typed` types. And `Typed` is automatically implemented
when deriving `Reflect` (as it is required).

The one downside is we do need to return `Option<&'static TypeInfo>`
from all these new methods so that we can handle the dynamic cases. If
we didn't have to, we'd be able to get rid of the `Option` entirely. But
I think that's an okay tradeoff for this one part of the API, and keeps
the other APIs intact.

## Testing

This PR contains tests to verify everything works as expected. You can
test locally by running:

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

---

## Changelog

### Public Changes

- Added `ArrayInfo::item_info` method
- Added `NamedField::type_info` method
- Added `UnnamedField::type_info` method
- Added `ListInfo::item_info` method
- Added `MapInfo::key_info` method
- Added `MapInfo::value_info` method
- All active fields now have a `Typed` bound (remember that this is
automatically satisfied for all types that derive `Reflect`)

### Internal Changes

- Added `MaybeTyped` trait

## Migration Guide

All active fields for reflected types (including lists, maps, tuples,
etc.), must implement `Typed`. For the majority of users this won't have
any visible impact.

However, users implementing `Reflect` manually may need to update their
types to implement `Typed` if they weren't already.

Additionally, custom dynamic types will need to implement the new hidden
`MaybeTyped` trait.
2024-07-15 00:40:07 +00:00
..
bevy_a11y Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_animation Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_app Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_asset Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_audio Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_color Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_core Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_core_pipeline Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_derive Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_dev_tools Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_diagnostic Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_dylib Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_dynamic_plugin Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_ecs Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_encase_derive Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_gilrs Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_gizmos Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_gltf Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_hierarchy Fixed #14248 and other URL issues (#14276) 2024-07-11 12:01:49 +00:00
bevy_input Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_internal bevy_reflect: Feature-gate function reflection (#14174) 2024-07-14 15:55:31 +00:00
bevy_log Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_macro_utils Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_math Add inverse_mul and inverse_transform_point for isometries (#14311) 2024-07-14 19:53:40 +00:00
bevy_mikktspace Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_pbr Register VisibleMeshEntities (#14320) 2024-07-15 00:06:43 +00:00
bevy_picking Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_ptr Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_reflect bevy_reflect: Nested TypeInfo getters (#13321) 2024-07-15 00:40:07 +00:00
bevy_render Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_scene Created an EventMutator for when you want to mutate an event before reading (#13818) 2024-07-08 14:53:06 +00:00
bevy_sprite Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_state Add error message if states schedule missing (usually because StatesPlugin hasn't been added) (#14160) 2024-07-14 15:56:16 +00:00
bevy_tasks Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_text Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_time Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_transform Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_ui Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_utils Bump Version after Release (#14219) 2024-07-08 12:54:08 +00:00
bevy_window Fix intra-doc links and make CI test them (#14076) 2024-07-11 13:08:31 +00:00
bevy_winit Dirty fix for App hanging when windows are invisible on WindowsOS (#14155) 2024-07-14 16:47:28 +00:00