Unify transparent wrapper types e.g. references#26
Merged
Conversation
Robbepop
reviewed
Nov 20, 2020
Contributor
Robbepop
left a comment
There was a problem hiding this comment.
LGTM. I like the simplicity of this solution.
The only thing I'd like is to rename the generic MetaType to something more concrete such as UnderlyingType, IdentityType or just Identity something that really describes its semantics.
Robbepop
approved these changes
Nov 20, 2020
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.
Background
A bug was discovered by @Robbepop with the following recursive type using
Box:This should result in a self-referential type definition in the type registry, instead it was creating two equivalent definitions:
TreeandBox<Tree>.The cause is that
any::TypeId::of<T>()is used to uniquely identify types, andTreeandBox<Tree>are different types and therefore have differentTypeIds. This regression was likely introduced in #3, which removed the original separation between a type id and its definition."Transparent" wrapper types in SCALE
Boxis an example of what inparity-scale-codecis known as a "wrapper" type. Such types are marked by theWrapperTypeEncodetrait:For our purposes that means that the SCALE encoded representation of
Tis the same as that ofBox<T>, therefore we consider it a "transparent" wrapper type. Such a type would be erased in the type registry since it has the same definition.Other types which implement
WrapperTypeEncodeareFaux specialization
EDIT: I abandoned this approach because it doesn't work with generics e.g.
GenericStruct<Box<GenericStruct>>Therefore what we need is to be able to say that
meta_type(T) == metatype(Box<T>) == metadtype(&T) ...). To do this, for any type which implementsWrapperTypeEncodewe need to identify byany::TypeId::of<T>()(the id of the wrapped type). This way the same type definition is used for the wrapped type and it is erased during type registration.The initial approach in this PR is adapted from the autoref based specialization. Here is a playground demonstrating the concept.
Adding an associated type to the
TypeInfotraitThis is a simple alternative, which adds the associated type
MetaTypetoTypeInfo. This will be set toSelfby the derive macro and for most primitive type definitions. For the transparent wrapper types described above it will be set to the underlying wrapped type e.g. theTinBox<T>.The associated type will then be used when constructing the metatype to get the unique id with
core::any::TypeId::of<T::MetaType>(). So for any typeT, the equivalentBox<T>will have the same type id, and there will appear only a single type definition in the type registry.