Trait TypeResolver

Source
pub trait TypeResolver {
    type TypeId: TypeId + 'static;
    type Error: Debug + Display;

    // Required method
    fn resolve_type<'this, V>(
        &'this self,
        type_id: Self::TypeId,
        visitor: V,
    ) -> Result<<V as ResolvedTypeVisitor<'this>>::Value, Self::Error>
       where V: ResolvedTypeVisitor<'this, TypeId = Self::TypeId>;
}
Expand description

This trait can be implemented for any type that is capable of describing how some type (identified by a TypeResolver::TypeId) is represented in terms of SCALE encoded bytes.

The naive way of implementing a trait like this would be to have a function that takes in a type ID and returns an enum which represents how the type is SCALE encoded. However, building such an enum to describe more complex types would likely involve allocating, which we’d rather avoid where possible. Instead, the approach taken here is to provide a set of callbacks to the TypeResolver::resolve_type() via a ResolvedTypeVisitor implementation. Exactly one of these callbacks is fired (and provided the necessary information) depending on the outcome of resolving a given type. This allows us to be far more flexible about how we hand back the required information, which avoids allocations.

For an example implementation, see the code in the [portable_registry] module.

Required Associated Types§

Source

type TypeId: TypeId + 'static

An identifier which points to some type that we’d like information on.

Source

type Error: Debug + Display

An error that can be handed back in the process of trying to resolve a type.

Required Methods§

Source

fn resolve_type<'this, V>( &'this self, type_id: Self::TypeId, visitor: V, ) -> Result<<V as ResolvedTypeVisitor<'this>>::Value, Self::Error>
where V: ResolvedTypeVisitor<'this, TypeId = Self::TypeId>,

Given a type ID, this calls a method on the provided ResolvedTypeVisitor describing the outcome of resolving the SCALE encoding information.

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§