[][src]Module scale_info::build

Builders for defining metadata for variant types (enums), and composite types (structs). They are designed to allow only construction of valid definitions.

In most cases we recommend using the scale-info-derive crate to auto generate the builder constructions.

Examples

Generic struct

struct Foo<T> {
    bar: T,
    data: u64,
}

impl<T> TypeInfo for Foo<T>
where
    T: TypeInfo + 'static,
{
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
            .type_params(vec![MetaType::new::<T>()])
            .composite(Fields::named()
                .field_of::<T>("bar", "T")
                .field_of::<u64>("data", "u64")
            )
    }
}

Tuple struct

struct Foo(u32, bool);

impl TypeInfo for Foo {
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
            .composite(Fields::unnamed()
                .field_of::<u32>("u32")
                .field_of::<bool>("bool")
            )
    }
}

Enum with fields

enum Foo<T>{
    A(T),
    B { f: u32 },
    C,
}

impl<T> TypeInfo for Foo<T>
where
    T: TypeInfo + 'static,
{
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
               .type_params(vec![MetaType::new::<T>()])
            .variant(
                Variants::with_fields()
                    .variant("A", Fields::unnamed().field_of::<T>("T"))
                    .variant("B", Fields::named().field_of::<u32>("f", "u32"))
                    .variant("C", Fields::unit())
            )
    }
}

Enum without fields

enum Foo {
    A,
    B,
    C = 33,
}

impl TypeInfo for Foo {
    type Identity = Self;

    fn type_info() -> Type {
        Type::builder()
            .path(Path::new("Foo", module_path!()))
            .variant(
                Variants::fieldless()
                    .variant("A", 1)
                    .variant("B", 2)
                    .variant("C", 33)
            )
    }
}

Modules

state

State types for type builders which require a Path

Structs

FieldsBuilder

Build a set of either all named (e.g. for a struct) or all unnamed (e.g. for a tuple struct)

TypeBuilder

Builds a Type

VariantsBuilder

Builds a definition of a variant type i.e an enum

Enums

Fieldless

Build a type where all variants have no fields and the discriminant can be directly chosen or accessed

Fields

Provides FieldsBuilder constructors

NamedFields

A fields builder only allows named fields (e.g. a struct)

NoFields

A fields builder has no fields (e.g. a unit struct)

NoVariants

Build a type with no variants.

UnnamedFields

A fields builder only allows unnamed fields (e.g. a tuple)

VariantFields

Build a type where at least one variant has fields.

Variants

Empty enum for VariantsBuilder constructors for the type builder DSL.