bevy_reflect: Add DynamicTyped trait#15108
Merged
alice-i-cecile merged 3 commits intobevyengine:mainfrom Sep 13, 2024
Merged
Conversation
alice-i-cecile
approved these changes
Sep 9, 2024
5c863d1 to
1bed9c8
Compare
Contributor
|
Very reasonable. |
SpecificProtagonist
approved these changes
Sep 13, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 PartialReflectvalues might be a dynamic type, butdyn Reflectvalues are guaranteed to not be a dynamic type.Therefore, we can start to add methods to
Reflectthat 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
DynamicTypedtrait.This trait is similar to
DynamicTypePathin that it provides a way to use the non-object-safeTypedtrait in an object-safe way.And since all types that derive
Reflectwill also deriveTyped, we can safely addDynamicTypedas a supertrait ofReflect. This allows us to use it when just given adyn Reflecttrait object.Testing
You can test locally by running:
Showcase
Reflectnow has a supertrait ofDynamicTyped, allowingTypeInfoto be retrieved from adyn Reflecttrait object without having to unwrap anything!Migration Guide
Reflectnow has a supertrait ofDynamicTyped. If you were manually implementingReflectand did not implementTyped, you will now need to do so.