mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
e512cb602c
# 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 |
||
---|---|---|
.. | ||
bevy_a11y | ||
bevy_animation | ||
bevy_app | ||
bevy_asset | ||
bevy_audio | ||
bevy_color | ||
bevy_core | ||
bevy_core_pipeline | ||
bevy_derive | ||
bevy_dev_tools | ||
bevy_diagnostic | ||
bevy_dylib | ||
bevy_dynamic_plugin | ||
bevy_ecs | ||
bevy_encase_derive | ||
bevy_gilrs | ||
bevy_gizmos | ||
bevy_gltf | ||
bevy_hierarchy | ||
bevy_input | ||
bevy_internal | ||
bevy_log | ||
bevy_macro_utils | ||
bevy_math | ||
bevy_mikktspace | ||
bevy_pbr | ||
bevy_picking | ||
bevy_ptr | ||
bevy_reflect | ||
bevy_render | ||
bevy_scene | ||
bevy_sprite | ||
bevy_state | ||
bevy_tasks | ||
bevy_text | ||
bevy_time | ||
bevy_transform | ||
bevy_ui | ||
bevy_utils | ||
bevy_window | ||
bevy_winit |