VersionedMetadataEntryVersion

Trait VersionedMetadataEntryVersion 

Source
pub trait VersionedMetadataEntryVersion {
    type Versioned: Versioned;
    type OwnedSborVariant;
    type BorrowedSborVariant<'a>
       where Self: 'a;

    const DISCRIMINATOR: u8;

    // Required methods
    fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_>;
    fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self
       where Self: Sized;
    fn into_versioned(self) -> Self::Versioned;
}

Required Associated Constants§

Required Associated Types§

Required Methods§

Source

fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_>

Can be used to encode the type as a variant under the Versioned type, without needing to clone, like this: encoder.encode(x.as_encodable_variant()).

Source

fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self
where Self: Sized,

Can be used to decode the type from an encoded variant, like this: X::from_decoded_variant(decoder.decode()?).

Source

fn into_versioned(self) -> Self::Versioned

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl VersionedMetadataEntryVersion for MetadataEntryV1

Source§

const DISCRIMINATOR: u8 = VersionedMetadataEntry_discriminators::LATEST_VERSION

Source§

type Versioned = VersionedMetadataEntry

Source§

type OwnedSborVariant = SborFixedEnumVariant<$crate::prelude::preinterpret! { [!set! #full_generics = $(< $( $lt $( : $clt $(+ $dlt )* )? $( = $deflt)? ),+ >)?] [!set! #impl_generics = $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?] [!set! #type_generics = $(< $( $lt ),+ >)?] [!set! #versioned_type = $versioned_name $(< $( $lt ),+ >)?] [!set! #versioned_type_path = $versioned_name $(::< $( $lt ),+ >)?] [!set! #versions_type = $versions_name $(< $( $lt ),+ >)?] [!set! #versions_type_path = $versions_name $(::< $( $lt ),+ >)?] [!set! #permit_sbor_attribute_alias = [!ident! $versioned_name _PermitSborAttributes]] #[allow(dead_code)] $vis type $latest_version_alias = $latest_version_type; use $crate::PermitSborAttributes as #permit_sbor_attribute_alias; #[derive(#permit_sbor_attribute_alias)] $(#[$attributes])* $($(#[$outer_attributes])*)? // Needs to go below $attributes so that a #[derive(Sbor)] in the attributes can see it. #[sbor(as_type = [!string! #versions_type])] /// If you wish to get access to match on the versions, use `.as_ref()` or `.as_mut()`. $vis struct $versioned_name #full_generics { inner: Option<#versions_type>, } impl #impl_generics #versioned_type { pub fn new(inner: #versions_type) -> Self { Self { inner: Some(inner), } } } impl #impl_generics AsRef<#versions_type> for #versioned_type { fn as_ref(&self) -> &#versions_type { self.inner.as_ref().unwrap() } } impl #impl_generics AsMut<#versions_type> for #versioned_type { fn as_mut(&mut self) -> &mut #versions_type { self.inner.as_mut().unwrap() } } impl #impl_generics From<#versions_type> for #versioned_type { fn from(value: #versions_type) -> Self { Self::new(value) } } impl #impl_generics From<#versioned_type> for #versions_type { fn from(value: #versioned_type) -> Self { value.inner.unwrap() } } impl #impl_generics Versioned for #versioned_type { type Versions = #versions_type; type LatestVersion = $latest_version_type; fn is_fully_updated(&self) -> bool { self.as_ref().is_fully_updated() } fn in_place_fully_update(&mut self) -> &mut Self { if !self.is_fully_updated() { let current = self.inner.take().unwrap(); self.inner = Some(current.fully_update()); } self } fn fully_update_and_into_latest_version(self) -> Self::LatestVersion { self.inner.unwrap().fully_update_and_into_latest_version() } /// Constructs the versioned enum from the latest content fn from_latest_version(latest: Self::LatestVersion) -> Self { Self::new(latest.into()) } fn as_latest_version(&self) -> Option<&Self::LatestVersion> { self.as_ref().as_latest_version() } fn as_latest_version_mut(&mut self) -> Option<&mut Self::LatestVersion> { self.as_mut().as_latest_version_mut() } fn as_versions(&self) -> &Self::Versions { self.as_ref() } fn as_versions_mut(&mut self) -> &mut Self::Versions { self.as_mut() } fn into_versions(self) -> Self::Versions { self.inner.unwrap() } fn from_versions(version: Self::Versions) -> Self { Self::new(version) } } [!set! #discriminators = [!ident! $versioned_name _discriminators]] #[allow(non_snake_case)] mod #discriminators { // The initial version of this tool used 0-indexed/off-by-one discriminators accidentally. // We're stuck with these now unfortunately... // But we make them explicit in case versions are skipped. $($( pub const [!ident! VERSION_ $version_num]: u8 = $version_num - 1; )*)? pub const LATEST_VERSION: u8 = $latest_version - 1; } #[derive(#permit_sbor_attribute_alias)] $(#[$attributes])* $($(#[$inner_attributes])*)? $vis enum $versions_name #full_generics { $($( #[sbor(discriminator(#discriminators::[!ident! VERSION_ $version_num]))] [!ident! V $version_num]($version_type), )*)? #[sbor(discriminator(#discriminators::LATEST_VERSION))] [!ident! V $latest_version]($latest_version_type), } #[allow(dead_code)] impl #impl_generics #versions_type { /// Returns if update happened, and the updated versioned enum. fn attempt_single_update(self) -> (bool, Self) { match self { $($( Self::[!ident! V $version_num](value) => (true, Self::[!ident! V $update_to_version_num](value.into())), )*)? this @ Self::[!ident! V $latest_version](_) => (false, this), } } fn fully_update(mut self) -> Self { loop { let (did_update, updated) = self.attempt_single_update(); if did_update { // We should try updating self = updated; } else { // We're at latest - return return updated; } } } #[allow(unreachable_patterns)] pub fn is_fully_updated(&self) -> bool { match self { Self::[!ident! V $latest_version](_) => true, _ => false, } } #[allow(irrefutable_let_patterns)] fn fully_update_and_into_latest_version(self) -> $latest_version_type { let Self::[!ident! V $latest_version](latest) = self.fully_update() else { panic!("Invalid resolved latest version not equal to latest type") }; return latest; } fn from_latest_version(latest: $latest_version_type) -> Self { Self::[!ident! V $latest_version](latest) } #[allow(unreachable_patterns)] fn as_latest_version(&self) -> Option<&$latest_version_type> { match self { Self::[!ident! V $latest_version](latest) => Some(latest), _ => None, } } #[allow(unreachable_patterns)] fn as_latest_version_mut(&mut self) -> Option<&mut $latest_version_type> { match self { Self::[!ident! V $latest_version](latest) => Some(latest), _ => None, } } pub fn into_versioned(self) -> #versioned_type { #versioned_type_path::new(self) } } $($( #[allow(dead_code)] impl #impl_generics From<$version_type> for #versions_type { fn from(value: $version_type) -> Self { Self::[!ident! V $version_num](value) } } #[allow(dead_code)] impl #impl_generics From<$version_type> for #versioned_type { fn from(value: $version_type) -> Self { Self::new(#versions_type_path::[!ident! V $version_num](value)) } } )*)? #[allow(dead_code)] impl #impl_generics From<$latest_version_type> for #versions_type { fn from(value: $latest_version_type) -> Self { Self::[!ident! V $latest_version](value) } } #[allow(dead_code)] impl #impl_generics From<$latest_version_type> for #versioned_type { fn from(value: $latest_version_type) -> Self { Self::new($versions_name::[!ident! V $latest_version](value)) } } // This trait is similar to `SborEnumVariantFor<X, Versioned>`, but it's nicer to use as // it's got a better name and can be implemented without needing a specific CustomValueKind. [!set! #version_trait = [!ident! $versioned_name Version]] #[allow(dead_code)] $vis trait #version_trait { // Note: We need to use an explicit associated type to capture the generics. type Versioned: sbor::Versioned; const DISCRIMINATOR: u8; type OwnedSborVariant; type BorrowedSborVariant<'a> where Self: 'a; /// Can be used to encode the type as a variant under the Versioned type, without /// needing to clone, like this: `encoder.encode(x.as_encodable_variant())`. fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_>; /// Can be used to decode the type from an encoded variant, like this: /// `X::from_decoded_variant(decoder.decode()?)`. fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self where Self: core::marker::Sized; fn into_versioned(self) -> Self::Versioned; } $($( impl #impl_generics #version_trait for $version_type { type Versioned = #versioned_type; const DISCRIMINATOR: u8 = #discriminators::[!ident! VERSION_ $version_num]; type OwnedSborVariant = sbor::SborFixedEnumVariant::<{ #discriminators::[!ident! VERSION_ $version_num] }, (Self,)>; type BorrowedSborVariant<'a> = sbor::SborFixedEnumVariant::<{ #discriminators::[!ident! VERSION_ $version_num] }, (&'a Self,)> where Self: 'a; fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_> { sbor::SborFixedEnumVariant::new((self,)) } fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self { variant.into_fields().0 } fn into_versioned(self) -> Self::Versioned { #versioned_type_path::new(self.into()) } } )*)? impl #impl_generics #version_trait for $latest_version_type { type Versioned = $versioned_name #type_generics; const DISCRIMINATOR: u8 = #discriminators::LATEST_VERSION; type OwnedSborVariant = sbor::SborFixedEnumVariant::<{ #discriminators::LATEST_VERSION }, (Self,)>; type BorrowedSborVariant<'a> = sbor::SborFixedEnumVariant::<{ #discriminators::LATEST_VERSION }, (&'a Self,)> where Self: 'a; fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_> { sbor::SborFixedEnumVariant::new((self,)) } fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self { variant.into_fields().0 } fn into_versioned(self) -> Self::Versioned { #versioned_type_path::new(self.into()) } } }, (GenericMetadataValue<UncheckedUrl, UncheckedOrigin>,)>

Source§

type BorrowedSborVariant<'a> = SborFixedEnumVariant<$crate::prelude::preinterpret! { [!set! #full_generics = $(< $( $lt $( : $clt $(+ $dlt )* )? $( = $deflt)? ),+ >)?] [!set! #impl_generics = $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?] [!set! #type_generics = $(< $( $lt ),+ >)?] [!set! #versioned_type = $versioned_name $(< $( $lt ),+ >)?] [!set! #versioned_type_path = $versioned_name $(::< $( $lt ),+ >)?] [!set! #versions_type = $versions_name $(< $( $lt ),+ >)?] [!set! #versions_type_path = $versions_name $(::< $( $lt ),+ >)?] [!set! #permit_sbor_attribute_alias = [!ident! $versioned_name _PermitSborAttributes]] #[allow(dead_code)] $vis type $latest_version_alias = $latest_version_type; use $crate::PermitSborAttributes as #permit_sbor_attribute_alias; #[derive(#permit_sbor_attribute_alias)] $(#[$attributes])* $($(#[$outer_attributes])*)? // Needs to go below $attributes so that a #[derive(Sbor)] in the attributes can see it. #[sbor(as_type = [!string! #versions_type])] /// If you wish to get access to match on the versions, use `.as_ref()` or `.as_mut()`. $vis struct $versioned_name #full_generics { inner: Option<#versions_type>, } impl #impl_generics #versioned_type { pub fn new(inner: #versions_type) -> Self { Self { inner: Some(inner), } } } impl #impl_generics AsRef<#versions_type> for #versioned_type { fn as_ref(&self) -> &#versions_type { self.inner.as_ref().unwrap() } } impl #impl_generics AsMut<#versions_type> for #versioned_type { fn as_mut(&mut self) -> &mut #versions_type { self.inner.as_mut().unwrap() } } impl #impl_generics From<#versions_type> for #versioned_type { fn from(value: #versions_type) -> Self { Self::new(value) } } impl #impl_generics From<#versioned_type> for #versions_type { fn from(value: #versioned_type) -> Self { value.inner.unwrap() } } impl #impl_generics Versioned for #versioned_type { type Versions = #versions_type; type LatestVersion = $latest_version_type; fn is_fully_updated(&self) -> bool { self.as_ref().is_fully_updated() } fn in_place_fully_update(&mut self) -> &mut Self { if !self.is_fully_updated() { let current = self.inner.take().unwrap(); self.inner = Some(current.fully_update()); } self } fn fully_update_and_into_latest_version(self) -> Self::LatestVersion { self.inner.unwrap().fully_update_and_into_latest_version() } /// Constructs the versioned enum from the latest content fn from_latest_version(latest: Self::LatestVersion) -> Self { Self::new(latest.into()) } fn as_latest_version(&self) -> Option<&Self::LatestVersion> { self.as_ref().as_latest_version() } fn as_latest_version_mut(&mut self) -> Option<&mut Self::LatestVersion> { self.as_mut().as_latest_version_mut() } fn as_versions(&self) -> &Self::Versions { self.as_ref() } fn as_versions_mut(&mut self) -> &mut Self::Versions { self.as_mut() } fn into_versions(self) -> Self::Versions { self.inner.unwrap() } fn from_versions(version: Self::Versions) -> Self { Self::new(version) } } [!set! #discriminators = [!ident! $versioned_name _discriminators]] #[allow(non_snake_case)] mod #discriminators { // The initial version of this tool used 0-indexed/off-by-one discriminators accidentally. // We're stuck with these now unfortunately... // But we make them explicit in case versions are skipped. $($( pub const [!ident! VERSION_ $version_num]: u8 = $version_num - 1; )*)? pub const LATEST_VERSION: u8 = $latest_version - 1; } #[derive(#permit_sbor_attribute_alias)] $(#[$attributes])* $($(#[$inner_attributes])*)? $vis enum $versions_name #full_generics { $($( #[sbor(discriminator(#discriminators::[!ident! VERSION_ $version_num]))] [!ident! V $version_num]($version_type), )*)? #[sbor(discriminator(#discriminators::LATEST_VERSION))] [!ident! V $latest_version]($latest_version_type), } #[allow(dead_code)] impl #impl_generics #versions_type { /// Returns if update happened, and the updated versioned enum. fn attempt_single_update(self) -> (bool, Self) { match self { $($( Self::[!ident! V $version_num](value) => (true, Self::[!ident! V $update_to_version_num](value.into())), )*)? this @ Self::[!ident! V $latest_version](_) => (false, this), } } fn fully_update(mut self) -> Self { loop { let (did_update, updated) = self.attempt_single_update(); if did_update { // We should try updating self = updated; } else { // We're at latest - return return updated; } } } #[allow(unreachable_patterns)] pub fn is_fully_updated(&self) -> bool { match self { Self::[!ident! V $latest_version](_) => true, _ => false, } } #[allow(irrefutable_let_patterns)] fn fully_update_and_into_latest_version(self) -> $latest_version_type { let Self::[!ident! V $latest_version](latest) = self.fully_update() else { panic!("Invalid resolved latest version not equal to latest type") }; return latest; } fn from_latest_version(latest: $latest_version_type) -> Self { Self::[!ident! V $latest_version](latest) } #[allow(unreachable_patterns)] fn as_latest_version(&self) -> Option<&$latest_version_type> { match self { Self::[!ident! V $latest_version](latest) => Some(latest), _ => None, } } #[allow(unreachable_patterns)] fn as_latest_version_mut(&mut self) -> Option<&mut $latest_version_type> { match self { Self::[!ident! V $latest_version](latest) => Some(latest), _ => None, } } pub fn into_versioned(self) -> #versioned_type { #versioned_type_path::new(self) } } $($( #[allow(dead_code)] impl #impl_generics From<$version_type> for #versions_type { fn from(value: $version_type) -> Self { Self::[!ident! V $version_num](value) } } #[allow(dead_code)] impl #impl_generics From<$version_type> for #versioned_type { fn from(value: $version_type) -> Self { Self::new(#versions_type_path::[!ident! V $version_num](value)) } } )*)? #[allow(dead_code)] impl #impl_generics From<$latest_version_type> for #versions_type { fn from(value: $latest_version_type) -> Self { Self::[!ident! V $latest_version](value) } } #[allow(dead_code)] impl #impl_generics From<$latest_version_type> for #versioned_type { fn from(value: $latest_version_type) -> Self { Self::new($versions_name::[!ident! V $latest_version](value)) } } // This trait is similar to `SborEnumVariantFor<X, Versioned>`, but it's nicer to use as // it's got a better name and can be implemented without needing a specific CustomValueKind. [!set! #version_trait = [!ident! $versioned_name Version]] #[allow(dead_code)] $vis trait #version_trait { // Note: We need to use an explicit associated type to capture the generics. type Versioned: sbor::Versioned; const DISCRIMINATOR: u8; type OwnedSborVariant; type BorrowedSborVariant<'a> where Self: 'a; /// Can be used to encode the type as a variant under the Versioned type, without /// needing to clone, like this: `encoder.encode(x.as_encodable_variant())`. fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_>; /// Can be used to decode the type from an encoded variant, like this: /// `X::from_decoded_variant(decoder.decode()?)`. fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self where Self: core::marker::Sized; fn into_versioned(self) -> Self::Versioned; } $($( impl #impl_generics #version_trait for $version_type { type Versioned = #versioned_type; const DISCRIMINATOR: u8 = #discriminators::[!ident! VERSION_ $version_num]; type OwnedSborVariant = sbor::SborFixedEnumVariant::<{ #discriminators::[!ident! VERSION_ $version_num] }, (Self,)>; type BorrowedSborVariant<'a> = sbor::SborFixedEnumVariant::<{ #discriminators::[!ident! VERSION_ $version_num] }, (&'a Self,)> where Self: 'a; fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_> { sbor::SborFixedEnumVariant::new((self,)) } fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self { variant.into_fields().0 } fn into_versioned(self) -> Self::Versioned { #versioned_type_path::new(self.into()) } } )*)? impl #impl_generics #version_trait for $latest_version_type { type Versioned = $versioned_name #type_generics; const DISCRIMINATOR: u8 = #discriminators::LATEST_VERSION; type OwnedSborVariant = sbor::SborFixedEnumVariant::<{ #discriminators::LATEST_VERSION }, (Self,)>; type BorrowedSborVariant<'a> = sbor::SborFixedEnumVariant::<{ #discriminators::LATEST_VERSION }, (&'a Self,)> where Self: 'a; fn as_encodable_variant(&self) -> Self::BorrowedSborVariant<'_> { sbor::SborFixedEnumVariant::new((self,)) } fn from_decoded_variant(variant: Self::OwnedSborVariant) -> Self { variant.into_fields().0 } fn into_versioned(self) -> Self::Versioned { #versioned_type_path::new(self.into()) } } }, (&'a GenericMetadataValue<UncheckedUrl, UncheckedOrigin>,)> where Self: 'a