bevy_reflect: TypeInfo
casting methods
#13320
Open
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
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:
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
,TypeInfo
now has methods for attempting a cast into the variant's info type.These new conversion methods return a
Result
where the error type is a newTypeInfoError
enum.A
Result
was chosen as the return type overOption
because if we do choose tounwrap
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 theelse
case.Open Questions
ReflectRef
and friends.ReflectRef
,ReflectMut
, andReflectOwned
? 馃try_as_***
instead ofas_***
since they return aResult
?Testing
You can test locally by running:
Changelog
Added
TypeInfoError
enumTypeInfo::kind
methodTypeInfo::as_struct
methodTypeInfo::as_tuple_struct
methodTypeInfo::as_tuple
methodTypeInfo::as_list
methodTypeInfo::as_array
methodTypeInfo::as_map
methodTypeInfo::as_enum
methodTypeInfo::as_value
methodVariantInfoError
enumVariantInfo::variant_type
methodVariantInfo::as_unit_variant
methodVariantInfo::as_tuple_variant
methodVariantInfo::as_struct_variant
method