mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 22:50:19 +00:00
229d6c686f
# Objective There are times where we want to simply take an owned `dyn Reflect` and cast it to a type `T`. Currently, this involves doing: ```rust let value = value.take::<T>().unwrap_or_else(|value| { T::from_reflect(&*value).unwrap_or_else(|| { panic!( "expected value of type {} to convert to type {}.", value.type_name(), std::any::type_name::<T>() ) }) }); ``` This is a common operation that could be easily be simplified. ## Solution Add the `FromReflect::take_from_reflect` method. This first tries to `take` the value, calling `from_reflect` iff that fails. ```rust let value = T::take_from_reflect(value).unwrap_or_else(|value| { panic!( "expected value of type {} to convert to type {}.", value.type_name(), std::any::type_name::<T>() ) }); ``` Based on suggestion from @soqb on [Discord](https://discord.com/channels/691052431525675048/1002362493634629796/1041046880316043374). --- ## Changelog - Add `FromReflect::take_from_reflect` method |
||
---|---|---|
.. | ||
bevy_animation | ||
bevy_app | ||
bevy_asset | ||
bevy_audio | ||
bevy_core | ||
bevy_core_pipeline | ||
bevy_derive | ||
bevy_diagnostic | ||
bevy_dylib | ||
bevy_dynamic_plugin | ||
bevy_ecs | ||
bevy_ecs_compile_fail_tests | ||
bevy_encase_derive | ||
bevy_gilrs | ||
bevy_gltf | ||
bevy_hierarchy | ||
bevy_input | ||
bevy_internal | ||
bevy_log | ||
bevy_macro_utils | ||
bevy_math | ||
bevy_mikktspace | ||
bevy_pbr | ||
bevy_ptr | ||
bevy_reflect | ||
bevy_reflect_compile_fail_tests | ||
bevy_render | ||
bevy_scene | ||
bevy_sprite | ||
bevy_tasks | ||
bevy_text | ||
bevy_time | ||
bevy_transform | ||
bevy_ui | ||
bevy_utils | ||
bevy_window | ||
bevy_winit |