From 61a62b74e479e81bb27e57d6058cfaf0b0ac4c4d Mon Sep 17 00:00:00 2001 From: dastansam Date: Sat, 4 May 2024 15:07:23 +0200 Subject: [PATCH 1/8] Use `versioned_type!` for `VersionedXcm` --- polkadot/xcm/src/lib.rs | 144 ++++++++++------------------------------ 1 file changed, 35 insertions(+), 109 deletions(-) diff --git a/polkadot/xcm/src/lib.rs b/polkadot/xcm/src/lib.rs index 513dfe5501ba..784b94ff8b38 100644 --- a/polkadot/xcm/src/lib.rs +++ b/polkadot/xcm/src/lib.rs @@ -77,6 +77,15 @@ pub trait TryAs { } macro_rules! versioned_type { + // only impl `MaxEncodedLen` for enums without generic type parameters + (@internal $n:ident, $v3:ty, ) => { + impl MaxEncodedLen for $n { + fn max_encoded_len() -> usize { + <$v3>::max_encoded_len() + } + } + }; + (@internal $n:ident, $v3:ty, $t:ident) => {}; ($(#[$attr:meta])* pub enum $n:ident { $(#[$index3:meta])+ V3($v3:ty), @@ -176,7 +185,7 @@ macro_rules! versioned_type { } }; - ($(#[$attr:meta])* pub enum $n:ident { + ($(#[$attr:meta])* pub enum $n:ident $(<$($gen:ident)*>)?{ $(#[$index2:meta])+ V2($v2:ty), $(#[$index3:meta])+ @@ -193,9 +202,10 @@ macro_rules! versioned_type { )] #[codec(encode_bound())] #[codec(decode_bound())] + $(#[scale_info(bounds(), skip_type_params($($gen)+))])? #[scale_info(replace_segment("staging_xcm", "xcm"))] $(#[$attr])* - pub enum $n { + pub enum $n $(<$($gen),+>)? { $(#[$index2])* V2($v2), $(#[$index3])* @@ -203,12 +213,12 @@ macro_rules! versioned_type { $(#[$index4])* V4($v4), } - impl $n { + impl$(<$($gen),+>)? $n $(<$($gen),+>)? { pub fn try_as(&self) -> Result<&T, ()> where Self: TryAs { >::try_as(&self) } } - impl TryAs<$v2> for $n { + impl$(<$($gen),+>)? TryAs<$v2> for $n $(<$($gen),+>)?{ fn try_as(&self) -> Result<&$v2, ()> { match &self { Self::V2(ref x) => Ok(x), @@ -216,7 +226,7 @@ macro_rules! versioned_type { } } } - impl TryAs<$v3> for $n { + impl$(<$($gen),+>)? TryAs<$v3> for $n $(<$($gen),+>)?{ fn try_as(&self) -> Result<&$v3, ()> { match &self { Self::V3(ref x) => Ok(x), @@ -224,7 +234,7 @@ macro_rules! versioned_type { } } } - impl TryAs<$v4> for $n { + impl$(<$($gen),+>)? TryAs<$v4> for $n $(<$($gen),+>)? { fn try_as(&self) -> Result<&$v4, ()> { match &self { Self::V4(ref x) => Ok(x), @@ -232,7 +242,7 @@ macro_rules! versioned_type { } } } - impl IntoVersion for $n { + impl$(<$($gen),+>)? IntoVersion for $n $(<$($gen),+>)?{ fn into_version(self, n: Version) -> Result { Ok(match n { 1 | 2 => Self::V2(self.try_into()?), @@ -242,19 +252,19 @@ macro_rules! versioned_type { }) } } - impl From<$v2> for $n { + impl$(<$($gen),+>)? From<$v2> for $n $(<$($gen),+>)? { fn from(x: $v2) -> Self { $n::V2(x) } } - impl> From for $n { + impl<$($($gen,),+)? T: Into<$v4>> From for $n $(<$($gen),+>)? { fn from(x: T) -> Self { $n::V4(x.into()) } } - impl TryFrom<$n> for $v2 { + impl$(<$($gen),+>)?TryFrom<$n $(<$($gen),+>)?> for $v2 { type Error = (); - fn try_from(x: $n) -> Result { + fn try_from(x: $n $(<$($gen),+>)?) -> Result { use $n::*; match x { V2(x) => Ok(x), @@ -266,9 +276,9 @@ macro_rules! versioned_type { } } } - impl TryFrom<$n> for $v3 { + impl$(<$($gen),+>)? TryFrom<$n $(<$($gen),+>)?> for $v3 { type Error = (); - fn try_from(x: $n) -> Result { + fn try_from(x: $n $(<$($gen),+>)?) -> Result { use $n::*; match x { V2(x) => x.try_into(), @@ -277,9 +287,9 @@ macro_rules! versioned_type { } } } - impl TryFrom<$n> for $v4 { + impl$(<$($gen),+>)?TryFrom<$n $(<$($gen),+>)?> for $v4 { type Error = (); - fn try_from(x: $n) -> Result { + fn try_from(x: $n $(<$($gen),+>)?) -> Result { use $n::*; match x { V2(x) => { @@ -291,12 +301,8 @@ macro_rules! versioned_type { } } } - impl MaxEncodedLen for $n { - fn max_encoded_len() -> usize { - <$v3>::max_encoded_len() - } - } - impl IdentifyVersion for $n { + versioned_type!(@internal $n, $v3, $($($gen),+)?); + impl$(<$($gen),+>)? IdentifyVersion for $n $(<$($gen),+>)? { fn identify_version(&self) -> Version { use $n::*; match self { @@ -416,40 +422,14 @@ versioned_type! { #[deprecated(note = "Use `VersionedAssets` instead")] pub type VersionedMultiAssets = VersionedAssets; -/// A single XCM message, together with its version code. -#[derive(Derivative, Encode, Decode, TypeInfo)] -#[derivative(Clone(bound = ""), Eq(bound = ""), PartialEq(bound = ""), Debug(bound = ""))] -#[codec(encode_bound())] -#[codec(decode_bound())] -#[scale_info(bounds(), skip_type_params(RuntimeCall))] -#[scale_info(replace_segment("staging_xcm", "xcm"))] -pub enum VersionedXcm { - #[codec(index = 2)] - V2(v2::Xcm), - #[codec(index = 3)] - V3(v3::Xcm), - #[codec(index = 4)] - V4(v4::Xcm), -} - -impl IntoVersion for VersionedXcm { - fn into_version(self, n: Version) -> Result { - Ok(match n { - 2 => Self::V2(self.try_into()?), - 3 => Self::V3(self.try_into()?), - 4 => Self::V4(self.try_into()?), - _ => return Err(()), - }) - } -} - -impl IdentifyVersion for VersionedXcm { - fn identify_version(&self) -> Version { - match self { - Self::V2(_) => v2::VERSION, - Self::V3(_) => v3::VERSION, - Self::V4(_) => v4::VERSION, - } +versioned_type! { + pub enum VersionedXcm { + #[codec(index = 2)] + V2(v2::Xcm), + #[codec(index = 3)] + V3(v3::Xcm), + #[codec(index = 4)] + V4(v4::Xcm), } } @@ -470,66 +450,12 @@ impl VersionedXcm { } } -impl From> for VersionedXcm { - fn from(x: v2::Xcm) -> Self { - VersionedXcm::V2(x) - } -} - impl From> for VersionedXcm { fn from(x: v3::Xcm) -> Self { VersionedXcm::V3(x) } } -impl From> for VersionedXcm { - fn from(x: v4::Xcm) -> Self { - VersionedXcm::V4(x) - } -} - -impl TryFrom> for v2::Xcm { - type Error = (); - fn try_from(x: VersionedXcm) -> Result { - use VersionedXcm::*; - match x { - V2(x) => Ok(x), - V3(x) => x.try_into(), - V4(x) => { - let v3: v3::Xcm = x.try_into()?; - v3.try_into() - }, - } - } -} - -impl TryFrom> for v3::Xcm { - type Error = (); - fn try_from(x: VersionedXcm) -> Result { - use VersionedXcm::*; - match x { - V2(x) => x.try_into(), - V3(x) => Ok(x), - V4(x) => x.try_into(), - } - } -} - -impl TryFrom> for v4::Xcm { - type Error = (); - fn try_from(x: VersionedXcm) -> Result { - use VersionedXcm::*; - match x { - V2(x) => { - let v3: v3::Xcm = x.try_into()?; - v3.try_into() - }, - V3(x) => x.try_into(), - V4(x) => Ok(x), - } - } -} - /// Convert an `Xcm` datum into a `VersionedXcm`, based on a destination `Location` which will /// interpret it. pub trait WrapVersion { From 37c8a6b4b4c1ab51e73843eba18bc54470c1942e Mon Sep 17 00:00:00 2001 From: dastansam Date: Sat, 4 May 2024 15:14:55 +0200 Subject: [PATCH 2/8] Add prdoc --- polkadot/xcm/src/lib.rs | 36 ++++++++++++++++++++++++------------ prdoc/pr_4378.prdoc | 15 +++++++++++++++ 2 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 prdoc/pr_4378.prdoc diff --git a/polkadot/xcm/src/lib.rs b/polkadot/xcm/src/lib.rs index 784b94ff8b38..083cf1f197f1 100644 --- a/polkadot/xcm/src/lib.rs +++ b/polkadot/xcm/src/lib.rs @@ -77,15 +77,37 @@ pub trait TryAs { } macro_rules! versioned_type { - // only impl `MaxEncodedLen` for enums without generic type parameters (@internal $n:ident, $v3:ty, ) => { + // only impl `MaxEncodedLen` for enums without generic type parameters impl MaxEncodedLen for $n { fn max_encoded_len() -> usize { <$v3>::max_encoded_len() } } + impl IntoVersion for $n { + fn into_version(self, n: Version) -> Result { + Ok(match n { + 1 | 2 => Self::V2(self.try_into()?), + 3 => Self::V3(self.try_into()?), + 4 => Self::V4(self.try_into()?), + _ => return Err(()), + }) + } + } + }; + (@internal $n:ident, $v3:ty, $t:ident) => { + // `VersionedXcm` doesn't have version 1, so we don't need to handle it. + impl<$t> IntoVersion for $n<$t>{ + fn into_version(self, n: Version) -> Result { + Ok(match n { + 2 => Self::V2(self.try_into()?), + 3 => Self::V3(self.try_into()?), + 4 => Self::V4(self.try_into()?), + _ => return Err(()), + }) + } + } }; - (@internal $n:ident, $v3:ty, $t:ident) => {}; ($(#[$attr:meta])* pub enum $n:ident { $(#[$index3:meta])+ V3($v3:ty), @@ -242,16 +264,6 @@ macro_rules! versioned_type { } } } - impl$(<$($gen),+>)? IntoVersion for $n $(<$($gen),+>)?{ - fn into_version(self, n: Version) -> Result { - Ok(match n { - 1 | 2 => Self::V2(self.try_into()?), - 3 => Self::V3(self.try_into()?), - 4 => Self::V4(self.try_into()?), - _ => return Err(()), - }) - } - } impl$(<$($gen),+>)? From<$v2> for $n $(<$($gen),+>)? { fn from(x: $v2) -> Self { $n::V2(x) diff --git a/prdoc/pr_4378.prdoc b/prdoc/pr_4378.prdoc new file mode 100644 index 000000000000..13b1182f275f --- /dev/null +++ b/prdoc/pr_4378.prdoc @@ -0,0 +1,15 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: "Use `versioned_type!` macro for `VersionedXcm`" + +doc: + - audience: Runtime Dev + description: | + Currently, all other versioned types in the `stagin-xcm` are created using `versioned_type!` macro, except for `VersionedXcm`. + This PR adds changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional + generic type param to an enum that is passed to the macro. + +crates: + - name: staging-xcm + bump: minor From 6cf04ba484996a58e29b2077efbcae204c5e5fbf Mon Sep 17 00:00:00 2001 From: dastansam Date: Mon, 6 May 2024 01:29:40 +0200 Subject: [PATCH 3/8] Try resolve comments --- polkadot/xcm/src/lib.rs | 57 +++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/polkadot/xcm/src/lib.rs b/polkadot/xcm/src/lib.rs index 083cf1f197f1..d991c804c5e7 100644 --- a/polkadot/xcm/src/lib.rs +++ b/polkadot/xcm/src/lib.rs @@ -77,13 +77,7 @@ pub trait TryAs { } macro_rules! versioned_type { - (@internal $n:ident, $v3:ty, ) => { - // only impl `MaxEncodedLen` for enums without generic type parameters - impl MaxEncodedLen for $n { - fn max_encoded_len() -> usize { - <$v3>::max_encoded_len() - } - } + (@internal $n:ident, $v3:ty, $v4:ty,) => { impl IntoVersion for $n { fn into_version(self, n: Version) -> Result { Ok(match n { @@ -94,9 +88,14 @@ macro_rules! versioned_type { }) } } + impl MaxEncodedLen for $n { + fn max_encoded_len() -> usize { + <$v3>::max_encoded_len().max(<$v4>::max_encoded_len()) + } + } }; - (@internal $n:ident, $v3:ty, $t:ident) => { - // `VersionedXcm` doesn't have version 1, so we don't need to handle it. + (@internal $n:ident, $v3:ty, $v4:ty, $t:ident) => { + // `VersionedXcm` version 1 support is dropped impl<$t> IntoVersion for $n<$t>{ fn into_version(self, n: Version) -> Result { Ok(match n { @@ -207,7 +206,7 @@ macro_rules! versioned_type { } }; - ($(#[$attr:meta])* pub enum $n:ident $(<$($gen:ident)*>)?{ + ($(#[$attr:meta])* pub enum $n:ident $(<$($gen:ident),*>)?{ $(#[$index2:meta])+ V2($v2:ty), $(#[$index3:meta])+ @@ -227,7 +226,7 @@ macro_rules! versioned_type { $(#[scale_info(bounds(), skip_type_params($($gen)+))])? #[scale_info(replace_segment("staging_xcm", "xcm"))] $(#[$attr])* - pub enum $n $(<$($gen),+>)? { + pub enum $n<$($($gen),*)?>{ $(#[$index2])* V2($v2), $(#[$index3])* @@ -235,12 +234,12 @@ macro_rules! versioned_type { $(#[$index4])* V4($v4), } - impl$(<$($gen),+>)? $n $(<$($gen),+>)? { + impl<$($($gen),*)?> $n<$($($gen),*)?> { pub fn try_as(&self) -> Result<&T, ()> where Self: TryAs { >::try_as(&self) } } - impl$(<$($gen),+>)? TryAs<$v2> for $n $(<$($gen),+>)?{ + impl<$($($gen),*)?> TryAs<$v2> for $n <$($($gen),*)?>{ fn try_as(&self) -> Result<&$v2, ()> { match &self { Self::V2(ref x) => Ok(x), @@ -248,7 +247,7 @@ macro_rules! versioned_type { } } } - impl$(<$($gen),+>)? TryAs<$v3> for $n $(<$($gen),+>)?{ + impl<$($($gen),*)?> TryAs<$v3> for $n <$($($gen),*)?>{ fn try_as(&self) -> Result<&$v3, ()> { match &self { Self::V3(ref x) => Ok(x), @@ -256,7 +255,7 @@ macro_rules! versioned_type { } } } - impl$(<$($gen),+>)? TryAs<$v4> for $n $(<$($gen),+>)? { + impl<$($($gen),*)?> TryAs<$v4> for $n<$($($gen),*)?> { fn try_as(&self) -> Result<&$v4, ()> { match &self { Self::V4(ref x) => Ok(x), @@ -264,19 +263,19 @@ macro_rules! versioned_type { } } } - impl$(<$($gen),+>)? From<$v2> for $n $(<$($gen),+>)? { + impl<$($($gen),*)?> From<$v2> for $n<$($($gen),*)?> { fn from(x: $v2) -> Self { $n::V2(x) } } - impl<$($($gen,),+)? T: Into<$v4>> From for $n $(<$($gen),+>)? { + impl<$($($gen,),+)? T: Into<$v4>> From for $n<$($($gen),*)?> { fn from(x: T) -> Self { $n::V4(x.into()) } } - impl$(<$($gen),+>)?TryFrom<$n $(<$($gen),+>)?> for $v2 { + impl<$($($gen),*)?>TryFrom<$n<$($($gen),*)?>> for $v2 { type Error = (); - fn try_from(x: $n $(<$($gen),+>)?) -> Result { + fn try_from(x: $n<$($($gen),*)?>) -> Result { use $n::*; match x { V2(x) => Ok(x), @@ -288,9 +287,9 @@ macro_rules! versioned_type { } } } - impl$(<$($gen),+>)? TryFrom<$n $(<$($gen),+>)?> for $v3 { + impl<$($($gen),*)?> TryFrom<$n<$($($gen),*)?>> for $v3 { type Error = (); - fn try_from(x: $n $(<$($gen),+>)?) -> Result { + fn try_from(x: $n <$($($gen),*)?>) -> Result { use $n::*; match x { V2(x) => x.try_into(), @@ -299,9 +298,9 @@ macro_rules! versioned_type { } } } - impl$(<$($gen),+>)?TryFrom<$n $(<$($gen),+>)?> for $v4 { + impl<$($($gen),*)?> TryFrom<$n <$($($gen),*)?>> for $v4 { type Error = (); - fn try_from(x: $n $(<$($gen),+>)?) -> Result { + fn try_from(x: $n<$($($gen),*)?>) -> Result { use $n::*; match x { V2(x) => { @@ -313,8 +312,10 @@ macro_rules! versioned_type { } } } - versioned_type!(@internal $n, $v3, $($($gen),+)?); - impl$(<$($gen),+>)? IdentifyVersion for $n $(<$($gen),+>)? { + // internal macro that handles some edge cases + versioned_type!(@internal $n, $v3, $v4, $($($gen),+)?); + + impl<$($($gen),*)?> IdentifyVersion for $n<$($($gen),*)?>{ fn identify_version(&self) -> Version { use $n::*; match self { @@ -462,12 +463,6 @@ impl VersionedXcm { } } -impl From> for VersionedXcm { - fn from(x: v3::Xcm) -> Self { - VersionedXcm::V3(x) - } -} - /// Convert an `Xcm` datum into a `VersionedXcm`, based on a destination `Location` which will /// interpret it. pub trait WrapVersion { From bda5a4cda269985230eaba9eb24742eb6b6071c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Wed, 24 Jul 2024 13:45:34 +0200 Subject: [PATCH 4/8] Update prdoc/pr_4378.prdoc --- prdoc/pr_4378.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4378.prdoc b/prdoc/pr_4378.prdoc index 13b1182f275f..351edebf4ec1 100644 --- a/prdoc/pr_4378.prdoc +++ b/prdoc/pr_4378.prdoc @@ -12,4 +12,4 @@ doc: crates: - name: staging-xcm - bump: minor + bump: patch From 06b9211e3af22784419751301f6657fe0be4d6dc Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Mon, 5 Aug 2024 09:55:12 +0200 Subject: [PATCH 5/8] Update prdoc/pr_4378.prdoc Co-authored-by: Adrian Catangiu --- prdoc/pr_4378.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4378.prdoc b/prdoc/pr_4378.prdoc index 351edebf4ec1..8c4e742ce313 100644 --- a/prdoc/pr_4378.prdoc +++ b/prdoc/pr_4378.prdoc @@ -6,7 +6,7 @@ title: "Use `versioned_type!` macro for `VersionedXcm`" doc: - audience: Runtime Dev description: | - Currently, all other versioned types in the `stagin-xcm` are created using `versioned_type!` macro, except for `VersionedXcm`. + Currently, all other versioned types in the `staging-xcm` are created using `versioned_type!` macro, except for `VersionedXcm`. This PR adds changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional generic type param to an enum that is passed to the macro. From bcc1db27185997477feb0cb70c44935d03ced703 Mon Sep 17 00:00:00 2001 From: Francisco Aguirre Date: Mon, 5 Aug 2024 09:55:19 +0200 Subject: [PATCH 6/8] Update prdoc/pr_4378.prdoc Co-authored-by: Adrian Catangiu --- prdoc/pr_4378.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4378.prdoc b/prdoc/pr_4378.prdoc index 8c4e742ce313..82380a7fe135 100644 --- a/prdoc/pr_4378.prdoc +++ b/prdoc/pr_4378.prdoc @@ -7,7 +7,7 @@ doc: - audience: Runtime Dev description: | Currently, all other versioned types in the `staging-xcm` are created using `versioned_type!` macro, except for `VersionedXcm`. - This PR adds changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional + This PR changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional generic type param to an enum that is passed to the macro. crates: From 9be1d47b66924a679a9379ba64efde4f89b7da47 Mon Sep 17 00:00:00 2001 From: dastansam Date: Sun, 12 Jan 2025 20:23:57 +0600 Subject: [PATCH 7/8] Fix prdoc --- prdoc/pr_4378.prdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prdoc/pr_4378.prdoc b/prdoc/pr_4378.prdoc index 82380a7fe135..c9faaef89d06 100644 --- a/prdoc/pr_4378.prdoc +++ b/prdoc/pr_4378.prdoc @@ -7,7 +7,7 @@ doc: - audience: Runtime Dev description: | Currently, all other versioned types in the `staging-xcm` are created using `versioned_type!` macro, except for `VersionedXcm`. - This PR changes `versioned_type` macro so that it can be used for `VersionedXcm` as well. This is done by adding optional + This PR changes `versioned_type` macro so that it can be used for `VersionedXcm` as well, reducing a lot of duplication. This is done by adding optional generic type param to an enum that is passed to the macro. crates: From 7548e788ba93f8bfc7bf941d658d271bc78aa712 Mon Sep 17 00:00:00 2001 From: dastansam Date: Sun, 12 Jan 2025 23:55:44 +0600 Subject: [PATCH 8/8] Remove generic type --- polkadot/xcm/src/lib.rs | 78 +++++++++++------------------------------ 1 file changed, 21 insertions(+), 57 deletions(-) diff --git a/polkadot/xcm/src/lib.rs b/polkadot/xcm/src/lib.rs index b0cd683762f4..e0d717b40794 100644 --- a/polkadot/xcm/src/lib.rs +++ b/polkadot/xcm/src/lib.rs @@ -82,38 +82,15 @@ pub trait TryAs { // NOTE: converting a v4 type into a versioned type will make it v5. macro_rules! versioned_type { (@internal $n:ident, $v3:ty, $v4:ty,) => { - impl IntoVersion for $n { - fn into_version(self, n: Version) -> Result { - Ok(match n { - 3 => Self::V3(self.try_into()?), - 4 => Self::V4(self.try_into()?), - 5 => Self::V5(self.try_into()?), - _ => return Err(()), - }) - } - } impl MaxEncodedLen for $n { fn max_encoded_len() -> usize { <$v3>::max_encoded_len().max(<$v4>::max_encoded_len()) } } }; - (@internal $n:ident, $v3:ty, $v4:ty, $t:ident $(, ($($bound_type:ty : $bound:path),+))?) => { - impl<$t> IntoVersion for $n<$t> - $(where $($bound_type : $bound),+)? - { - fn into_version(self, n: Version) -> Result { - Ok(match n { - 3 => Self::V3(self.try_into()?), - 4 => Self::V4(self.try_into()?), - 5 => Self::V5(self.try_into()?), - _ => return Err(()), - }) - } - } + (@internal $n:ident, $v3:ty, $v4:ty, $t:ident) => { }; - ($(#[$attr:meta])* pub enum $n:ident$(<$($gen:ident),*>)? $(where $($bound_type:ty : $bound_trait:path),+)? - { + ($(#[$attr:meta])* pub enum $n:ident$(<$($gen:ident),*>)? { $(#[$index3:meta])+ V3($v3:ty), $(#[$index4:meta])+ @@ -133,11 +110,7 @@ macro_rules! versioned_type { $(#[scale_info(bounds(), skip_type_params($($gen)+))])? #[scale_info(replace_segment("staging_xcm", "xcm"))] $(#[$attr])* - pub enum $n<$($($gen),*)?> - $( - where $($bound_type: $bound_trait),+ - )? - { + pub enum $n<$($($gen),*)?> { $(#[$index3])* V3($v3), $(#[$index4])* @@ -146,18 +119,12 @@ macro_rules! versioned_type { V5($v5), } impl<$($($gen),*)?> $n<$($($gen),*)?> - $( - where $($bound_type: $bound_trait),+ - )? { pub fn try_as(&self) -> Result<&T, ()> where Self: TryAs { >::try_as(&self) } } impl<$($($gen),*)?> TryAs<$v3> for $n <$($($gen),*)?> - $( - where $($bound_type: $bound_trait),+ - )? { fn try_as(&self) -> Result<&$v3, ()> { match &self { @@ -167,9 +134,6 @@ macro_rules! versioned_type { } } impl<$($($gen),*)?> TryAs<$v4> for $n<$($($gen),*)?> - $( - where $($bound_type: $bound_trait),+ - )? { fn try_as(&self) -> Result<&$v4, ()> { match &self { @@ -179,9 +143,6 @@ macro_rules! versioned_type { } } impl<$($($gen),*)?> TryAs<$v5> for $n<$($($gen),*)?> - $( - where $($bound_type: $bound_trait),+ - )? { fn try_as(&self) -> Result<&$v5, ()> { match &self { @@ -191,18 +152,12 @@ macro_rules! versioned_type { } } impl<$($($gen),*)?> From<$v3> for $n <$($($gen),*)?> - $( - where $($bound_type: $bound_trait),+ - )? { fn from(x: $v3) -> Self { $n::V3(x.into()) } } impl<$($($gen,),+)? T: Into<$v5>> From for $n<$($($gen),*)?> - $( - where $($bound_type: $bound_trait),+ - )? { fn from(x: T) -> Self { $n::V5(x.into()) @@ -210,7 +165,7 @@ macro_rules! versioned_type { } impl<$($($gen),*)?> TryFrom<$n<$($($gen),*)?>> for $v3 $( - where $($bound_type: $bound_trait),+ + where $($gen: Decode + GetDispatchInfo),* )? { type Error = (); @@ -228,7 +183,7 @@ macro_rules! versioned_type { } impl<$($($gen),*)?> TryFrom<$n <$($($gen),*)?>> for $v4 $( - where $($bound_type: $bound_trait),+ + where $($gen: Decode + GetDispatchInfo),* )? { type Error = (); @@ -242,9 +197,6 @@ macro_rules! versioned_type { } } impl<$($($gen),*)?> TryFrom<$n<$($($gen),*)?>> for $v5 - $( - where $($bound_type: $bound_trait),+ - )? { type Error = (); fn try_from(x: $n<$($($gen),*)?>) -> Result { @@ -260,12 +212,24 @@ macro_rules! versioned_type { } } // internal macro that handles some edge cases - versioned_type!(@internal $n, $v3, $v4, $($($gen),+)? $(, ($($bound_type : $bound_trait),+))?); + versioned_type!(@internal $n, $v3, $v4, $($($gen),+)?); - impl<$($($gen),*)?> IdentifyVersion for $n<$($($gen),*)?> + impl<$($($gen),*)?> IntoVersion for $n<$($($gen),*)?> $( - where $($bound_type: $bound_trait),+ + where $($gen: Decode + GetDispatchInfo),* )? + { + fn into_version(self, n: Version) -> Result { + Ok(match n { + 3 => Self::V3(self.try_into()?), + 4 => Self::V4(self.try_into()?), + 5 => Self::V5(self.try_into()?), + _ => return Err(()), + }) + } + } + + impl<$($($gen),*)?> IdentifyVersion for $n<$($($gen),*)?> { fn identify_version(&self) -> Version { use $n::*; @@ -380,7 +344,7 @@ versioned_type! { pub type VersionedMultiAssets = VersionedAssets; versioned_type! { - pub enum VersionedXcm where RuntimeCall: Decode, RuntimeCall: GetDispatchInfo { + pub enum VersionedXcm { #[codec(index = 3)] V3(v3::Xcm), #[codec(index = 4)]