This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Use proc macros to generate conversion functions for MultiLocation #3635
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
13ce8a6
Use proc macros to generate conversion functions for MultiLocation
KiChjang 4bd110c
Add compile test and missing conversion cases
KiChjang bf7e03a
Add common derives for Parent and Ancestor
KiChjang f36810d
Generate conversion functions for MultiLocation v0 via proc macro
KiChjang 048b886
Add type conversion test and fix a bug
KiChjang ca45e21
cargo fmt
KiChjang 08740f3
Do not hardcode 8 as the number of max parents
KiChjang 780af41
Use map instead of for loops when generating code fragments
KiChjang 68c1b3f
Spelling
KiChjang 2aae220
cargo fmt
KiChjang d5e8c15
More mapping, less for-looping
KiChjang a7e2379
Merge remote-tracking branch 'origin/master' into kckyeung/xcm-proc-m…
KiChjang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| authors = ["Parity Technologies <[email protected]>"] | ||
| name = "xcm-procedural" | ||
| version = "0.1.0" | ||
| edition = "2018" | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| proc-macro2 = "1.0.28" | ||
| quote = "1.0.9" | ||
| syn = "1.0.74" |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright 2020 Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| //! Procedural macros used in XCM. | ||
|
|
||
| use proc_macro::TokenStream; | ||
|
|
||
| mod multilocation; | ||
|
|
||
| #[proc_macro] | ||
| pub fn impl_conversion_functions_for_multilocation_v1(input: TokenStream) -> TokenStream { | ||
| multilocation::generate_v1_conversion_functions(input) | ||
| .unwrap_or_else(syn::Error::into_compile_error) | ||
| .into() | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // Copyright 2020 Parity Technologies (UK) Ltd. | ||
| // This file is part of Polkadot. | ||
|
|
||
| // Polkadot is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
|
|
||
| // Polkadot is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
|
|
||
| // You should have received a copy of the GNU General Public License | ||
| // along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| use proc_macro2::{Span, TokenStream}; | ||
| use quote::{format_ident, quote}; | ||
| use syn::{Result, Token}; | ||
|
|
||
| pub fn generate_v1_conversion_functions(input: proc_macro::TokenStream) -> Result<TokenStream> { | ||
| if !input.is_empty() { | ||
| return Err(syn::Error::new(Span::call_site(), "No arguments expected")) | ||
| } | ||
|
|
||
| let from_tuples = generate_conversion_from_tuples(); | ||
| let from_v0 = generate_conversion_from_v0(); | ||
|
|
||
| Ok(quote! { | ||
| #from_tuples | ||
| #from_v0 | ||
| }) | ||
| } | ||
|
|
||
| fn generate_conversion_from_tuples() -> TokenStream { | ||
| let mut from_tuples = TokenStream::new(); | ||
|
|
||
| for num_junctions in 0..8usize { | ||
| let junctions = (0..=num_junctions).map(|_| format_ident!("Junction")).collect::<Vec<_>>(); | ||
| let idents = (0..=num_junctions).map(|i| format_ident!("j{}", i)).collect::<Vec<_>>(); | ||
| let variant = &format_ident!("X{}", num_junctions + 1); | ||
| let array_size = num_junctions + 1; | ||
|
|
||
| let from_tuple = quote! { | ||
| impl From<( #(#junctions,)* )> for MultiLocation { | ||
| fn from( ( #(#idents,)* ): ( #(#junctions,)* ) ) -> Self { | ||
| MultiLocation { parents: 0, interior: Junctions::#variant( #(#idents),* ) } | ||
| } | ||
| } | ||
|
|
||
| impl From<(u8, #(#junctions),*)> for MultiLocation { | ||
| fn from( ( parents, #(#idents),* ): (u8, #(#junctions),* ) ) -> Self { | ||
| MultiLocation { parents, interior: Junctions::#variant( #(#idents),* ) } | ||
| } | ||
| } | ||
|
|
||
| impl From<(Ancestor, #(#junctions),*)> for MultiLocation { | ||
| fn from( ( Ancestor(parents), #(#idents),* ): (Ancestor, #(#junctions),* ) ) -> Self { | ||
| MultiLocation { parents, interior: Junctions::#variant( #(#idents),* ) } | ||
| } | ||
| } | ||
|
|
||
| impl From<[Junction; #array_size]> for MultiLocation { | ||
| fn from(j: [Junction; #array_size]) -> Self { | ||
| let [#(#idents),*] = j; | ||
| MultiLocation { parents: 0, interior: Junctions::#variant( #(#idents),* ) } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Support up to 8 Parents in a tuple | ||
| for cur_parents in 1..=8u8 { | ||
| let parents = (0..cur_parents).map(|_| format_ident!("Parent")).collect::<Vec<_>>(); | ||
| let underscores = | ||
| (0..cur_parents).map(|_| Token)).collect::<Vec<_>>(); | ||
|
|
||
| from_tuples.extend(quote! { | ||
| impl From<( #(#parents,)* #(#junctions),* )> for MultiLocation { | ||
| fn from( (#(#underscores,)* #(#idents),*): ( #(#parents,)* #(#junctions),* ) ) -> Self { | ||
| MultiLocation { parents: #cur_parents, interior: Junctions::#variant( #(#idents),* ) } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| from_tuples.extend(from_tuple); | ||
| } | ||
|
|
||
| // Support up to 8 Parents in a tuple | ||
| for cur_parents in 1..=8u8 { | ||
| let parents = (0..cur_parents).map(|_| format_ident!("Parent")).collect::<Vec<_>>(); | ||
| let underscores = | ||
| (0..cur_parents).map(|_| Token)).collect::<Vec<_>>(); | ||
|
|
||
| from_tuples.extend(quote! { | ||
| impl From<( #(#parents,)* Junctions )> for MultiLocation { | ||
| fn from( (#(#underscores,)* junctions): ( #(#parents,)* Junctions ) ) -> Self { | ||
| MultiLocation { parents: #cur_parents, interior: junctions } | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| quote! { | ||
| impl From<Junctions> for MultiLocation { | ||
| fn from(junctions: Junctions) -> Self { | ||
| MultiLocation { parents: 0, interior: junctions } | ||
| } | ||
| } | ||
|
|
||
| impl From<(u8, Junctions)> for MultiLocation { | ||
| fn from((parents, interior): (u8, Junctions)) -> Self { | ||
| MultiLocation { parents, interior } | ||
| } | ||
| } | ||
|
|
||
| impl From<(Ancestor, Junctions)> for MultiLocation { | ||
| fn from((Ancestor(parents), interior): (Ancestor, Junctions)) -> Self { | ||
| MultiLocation { parents, interior } | ||
| } | ||
| } | ||
|
|
||
| impl From<()> for MultiLocation { | ||
| fn from(_: ()) -> Self { | ||
| MultiLocation { parents: 0, interior: Junctions::Here } | ||
| } | ||
| } | ||
|
|
||
| impl From<(u8,)> for MultiLocation { | ||
| fn from((parents,): (u8,)) -> Self { | ||
| MultiLocation { parents, interior: Junctions::Here } | ||
| } | ||
| } | ||
|
|
||
| impl From<Junction> for MultiLocation { | ||
| fn from(x: Junction) -> Self { | ||
| MultiLocation { parents: 0, interior: Junctions::X1(x) } | ||
| } | ||
| } | ||
|
|
||
| impl From<[Junction; 0]> for MultiLocation { | ||
| fn from(_: [Junction; 0]) -> Self { | ||
| MultiLocation { parents: 0, interior: Junctions::Here } | ||
| } | ||
| } | ||
apopiak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #from_tuples | ||
| } | ||
| } | ||
|
|
||
| fn generate_conversion_from_v0() -> TokenStream { | ||
| let mut match_variants = TokenStream::new(); | ||
|
|
||
| for cur_num in 0..8u8 { | ||
| let mut intermediate_match_arms = TokenStream::new(); | ||
| let num_ancestors = cur_num + 1; | ||
| let variant = format_ident!("X{}", num_ancestors); | ||
| let idents = (0..=cur_num).map(|i| format_ident!("j{}", i)).collect::<Vec<_>>(); | ||
|
|
||
| for parent_count in (1..num_ancestors).rev() { | ||
| let parent_idents = | ||
| (0..parent_count).map(|j| format_ident!("j{}", j)).collect::<Vec<_>>(); | ||
| let junction_idents = (parent_count..num_ancestors) | ||
| .map(|j| format_ident!("j{}", j)) | ||
| .collect::<Vec<_>>(); | ||
| let junction_variant = format_ident!("X{}", num_ancestors - parent_count); | ||
|
|
||
| intermediate_match_arms.extend(quote! { | ||
| crate::v0::MultiLocation::#variant( #(#idents),* ) | ||
| if #( #parent_idents.is_parent() )&&* => | ||
| Ok(MultiLocation { | ||
| parents: #parent_count, | ||
| interior: #junction_variant( #( #junction_idents.try_into()? ),* ), | ||
| }), | ||
| }); | ||
| } | ||
|
|
||
| match_variants.extend(quote! { | ||
| crate::v0::MultiLocation::#variant( #(#idents),* ) | ||
| if #( #idents.is_parent() )&&* => | ||
| Ok(MultiLocation::ancestor(#num_ancestors)), | ||
| #intermediate_match_arms | ||
| crate::v0::MultiLocation::#variant( #(#idents),* ) => | ||
| Ok( #variant( #( #idents.try_into()? ),* ).into() ), | ||
| }); | ||
| } | ||
|
|
||
| quote! { | ||
| impl TryFrom<crate::v0::MultiLocation> for MultiLocation { | ||
| type Error = (); | ||
| fn try_from(v0: crate::v0::MultiLocation) -> core::result::Result<Self, ()> { | ||
| use Junctions::*; | ||
| match v0 { | ||
| crate::v0::MultiLocation::Null => Ok(Here.into()), | ||
| #match_variants | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.