Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erase PhantomData fields #111

Merged
merged 5 commits into from
Jul 15, 2021
Merged

Erase PhantomData fields #111

merged 5 commits into from
Jul 15, 2021

Conversation

ascjones
Copy link
Contributor

Feedback from @jacogr while working on polkadot-js/api#3759.

He rightly points out that PhantomData fields are redundant from a scale encoding perspective, since nothing is actually encoded. So all type generators for different langs would have to implement a filter for them or have a bunch of useless fields. This is a Rust implementation detail leaking out into the metadata definition. This PR removes that burden and filters out fields of PhantomData at source.

I know there has been much discussion of this in the past, but I think this solution avoids the main problem we had in the past by avoiding the macro having to syntactically identify PhantomData types.

Let me know what you think @Robbepop @dvdplm

@Robbepop
Copy link
Contributor

Do I understand correctly that this PR filters out PhantomData types type safely?

@ascjones
Copy link
Contributor Author

Do I understand correctly that this PR filters out PhantomData types type safely?

Precisely

Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full circle then! :)

@jacogr is right of course but the reason we had PhantomData here was that we decided to put the onus on the devs to #[scale(skip)] such types, so they should never appear in metadata (and thus not create issues for different languages) if we're disciplined.

That said I don't disagree removing them.

@@ -91,7 +91,7 @@ impl IntoPortable for TypeDefComposite {

impl TypeDefComposite {
/// Creates a new struct definition with named fields.
pub fn new<I>(fields: I) -> Self
pub(crate) fn new<I>(fields: I) -> Self
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So users can't instantiate with any fields containing a PhantomData. They are filtered out in the builder, and this would allow circumventing the builder.

The other option of course would be to do the phantom filtering directly in this method, but I think we should encourage the use of the builder as the way to construct Composite types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants