bevy_reflect: Add DynamicTyped
trait
#15108
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Thanks to #7207, we now have a way to validate at the type-level that a reflected value is actually the type it says it is and not just a dynamic representation of that type.
dyn PartialReflect
values might be a dynamic type, butdyn Reflect
values are guaranteed to not be a dynamic type.Therefore, we can start to add methods to
Reflect
that weren't really possible before. For example, we should now be able to always get a&'static TypeInfo
, and not just anOption<&'static TypeInfo>
.Solution
Add the
DynamicTyped
trait.This trait is similar to
DynamicTypePath
in that it provides a way to use the non-object-safeTyped
trait in an object-safe way.And since all types that derive
Reflect
will also deriveTyped
, we can safely addDynamicTyped
as a supertrait ofReflect
. This allows us to use it when just given adyn Reflect
trait object.Testing
You can test locally by running:
Showcase
Reflect
now has a supertrait ofDynamicTyped
, allowingTypeInfo
to be retrieved from adyn Reflect
trait object without having to unwrap anything!Migration Guide
Reflect
now has a supertrait ofDynamicTyped
. If you were manually implementingReflect
and did not implementTyped
, you will now need to do so.