bevy/crates
Gino Valente e512cb602c
bevy_reflect: TypeInfo casting methods (#13320)
# Objective

There are times when we might know the type of a `TypeInfo` ahead of
time. Or we may have already checked it one way or another.

In such cases, it's a bit cumbersome to have to pattern match every time
we want to access the nested info:

```rust
if let TypeInfo::List(info) = <Vec<i32>>::type_info() {
  // ...
} else {
  panic!("expected list info");
}
```

Ideally, there would be a way to simply perform the cast down to
`ListInfo` since we already know it will succeed.

Or even if we don't, perhaps we just want a cleaner way of exiting a
function early (i.e. with the `?` operator).

## Solution

Taking a bit from
[`mirror-mirror`](https://docs.rs/mirror-mirror/latest/mirror_mirror/struct.TypeDescriptor.html#implementations),
`TypeInfo` now has methods for attempting a cast into the variant's info
type.

```rust
let info = <Vec<i32>>::type_info().as_list().unwrap();
// ...
```

These new conversion methods return a `Result` where the error type is a
new `TypeInfoError` enum.

A `Result` was chosen as the return type over `Option` because if we do
choose to `unwrap` it, the error message will give us some indication of
what went wrong. In other words, it can truly replace those instances
where we were panicking in the `else` case.

### Open Questions

1. Should the error types instead be a struct? I chose an enum for
future-proofing, but right now it only has one error state.
Alternatively, we could make it a reflect-wide casting error so it could
be used for similar methods on `ReflectRef` and friends.
2. I was going to do it in a separate PR but should I just go ahead and
add similar methods to `ReflectRef`, `ReflectMut`, and `ReflectOwned`? 🤔
3. Should we name these `try_as_***` instead of `as_***` since they
return a `Result`?

## Testing

You can test locally by running:

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

---

## Changelog

### Added

- `TypeInfoError` enum
- `TypeInfo::kind` method
- `TypeInfo::as_struct` method
- `TypeInfo::as_tuple_struct` method
- `TypeInfo::as_tuple` method
- `TypeInfo::as_list` method
- `TypeInfo::as_array` method
- `TypeInfo::as_map` method
- `TypeInfo::as_enum` method
- `TypeInfo::as_value` method
- `VariantInfoError` enum
- `VariantInfo::variant_type` method
- `VariantInfo::as_unit_variant` method
- `VariantInfo::as_tuple_variant` method
- `VariantInfo::as_struct_variant` method
2024-07-14 20:10:31 +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 Fix prepass batch (#13943) 2024-07-14 19:35:36 +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: TypeInfo casting methods (#13320) 2024-07-14 20:10:31 +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