ChainTypeRegistry

Struct ChainTypeRegistry 

Source
pub struct ChainTypeRegistry { /* private fields */ }
Expand description

A registry of types that’s been defined for a specific chain.

Use ChainTypeRegistry::for_spec_version() to get back something that implements scale_type_resolver::TypeResolver. Use serde to deserialize something into this struct (the deserialization logic is tuned to work best with serde_json, but any self describing format should work so long as it’s the right shape).

§Example

use scale_info_legacy::ChainTypeRegistry;

let json = serde_json::json!({
    // Types that are present globally, regardless of spec version:
    "global": {
        // Types here exist across all pallets.
        "types": {
            // A simple type alias:
            "Foo": "u8",
            // A tuple:
            "TupleOf": "(bool, Vec<String>)",
            // An unnamed struct (basically a tuple like "(bool, Vec<String>)" but with a path):
            "UnnamedStructOf": ["bool", "Vec<String>"],
            // A struct with 2 fields, a and b, and a generic type.
            "StructOf<T>": {
                "a": "bool",
                "b": "T"
            },
            // The simplest way to list enum variants when none have associated data:
            "EnumWithoutData": {
                "_enum": ["One", "Two", "Three"]
            },
            // We can also use a struct form to specify the data that each variant has:
            "EnumWithData": {
                "_enum": {
                    "A": "u64",
                    "B": ["bool", "char"],
                    "C": { "field_a": "String", "field_b": "bool" }
                }
            },
            // We can be very explicit if we want to specify the enum variant indexes:
            "EnumWithExplicitDetails": {
                "_enum": [
                    { "name": "A", "index": 0, "fields": "u64" },
                    { "name": "B", "index": 1, "fields": ["bool", "char"] },
                    { "name": "C", "index": 2, "fields": { "field_a": "String", "field_b": "bool" } }
                ]
            }
        },
        // Any type in palletTypes only exists within a certain pallet.
        "palletTypes": {
            // The Balance type only exists in the balances pallet.
            "balances": {
                "Balance": "u64"
            },
            // Fee and AssetsEnum only exist in the assets pallet.
            "assets": {
                "Fee": "u32",
                "AssetsEnum": {
                    "_enum": ["One", "Two", "Three"]
                }
            }
        },
        // We can also define runtime APIs. For each runtime API we point to
        // types declared elsewhere, rather than defining any new types.
        "runtimeApis": {
            "Metadata": {
                "metadata_versions": {
                    "inputs": [],
                    "output": "Vec<u32>"
                },
                "metadata_at_version": {
                    "inputs": ["u32"],
                    "output": "Option<Vec<u8>>"
                }
            }
        },
    },
    // We can define types that are only relevant in a specific spec range.
    // We can have overlaps here; later definitions trump earlier ones if so.
    "forSpec": [
        {
            // From 0-1000 (inclusive), we'll use these types.
            "range": [null, 1000],
            "types": {
                "Foo": "u64",
                "UnnamedStructOf": ["bool", "Vec<String>"],
                "StructOf<T>": { "a": "bool", "b": "T" },
            },
            "palletTypes": {
                "balances": {
                    "Balance": "u128"
                },
            }
        },
        {
            // From 1001-2000 (inclusive), we'll use these types.
            "range": [1001, 2000],
            "types": {
                "Foo": "String",
                "UnnamedStructOf": ["bool", "Vec<String>"],
                "StructOf<T>": { "a": "bool", "b": "T" },
            },
            // Runtime APIs can also be defined per spec range.
            "runtimeApis": {
                "Core": {
                    "version": {
                        "inputs": [],
                        "output": "RuntimeVersion"
                    }
                }
            }
        }
    ]
});

let tys: ChainTypeRegistry = serde_json::from_value(json).unwrap();
let resolver = tys.for_spec_version(12345);

Implementations§

Source§

impl ChainTypeRegistry

Source

pub fn empty() -> Self

Create a new empty ChainTypeRegistry. This does not allocate and can be used as a placeholder when no type information is available.

Source

pub fn for_spec_version(&self, spec_version: u64) -> TypeRegistrySet<'_>

Hand back a TypeRegistrySet that is able to resolve types for the given spec version.

Source

pub fn spec_version_ranges(&self) -> impl Iterator<Item = (u64, u64)> + use<'_>

Return the specific spec version ranges that we’ve defined types for.

Source

pub fn extend(&mut self, other: ChainTypeRegistry)

Extend this chain type registry with the one provided. In case of any matches, the provided types will overwrite the existing ones.

Trait Implementations§

Source§

impl Debug for ChainTypeRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for ChainTypeRegistry

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,