Derive Macro scale_encode::EncodeAsType

source ·
#[derive(EncodeAsType)]
{
    // Attributes available to this derive:
    #[encode_as_type]
    #[codec]
}
Expand description

The EncodeAsType derive macro can be used to implement EncodeAsType on structs and enums whose fields all implement EncodeAsType.

Examples

This can be applied to structs and enums:

use scale_encode::EncodeAsType;

#[derive(EncodeAsType)]
struct Foo(String);

#[derive(EncodeAsType)]
struct Bar {
    a: u64,
    b: bool
}

#[derive(EncodeAsType)]
enum Wibble<T> {
    A(usize, bool, T),
    B { value: String },
    C
}

If you aren’t directly depending on scale_encode, you must tell the macro what the path to it is so that it knows how to generate the relevant impls:

use alt_path::EncodeAsType;

#[derive(EncodeAsType)]
#[encode_as_type(crate_path = "alt_path")]
struct Foo<T> {
   a: u64,
   b: T
}

If you use generics, the macro will assume that each of them also implements EncodeAsType. This can be overridden when it’s not the case (the compiler will ensure that you can’t go wrong here):

use scale_encode::EncodeAsType;

#[derive(EncodeAsType)]
#[encode_as_type(trait_bounds = "")]
struct Foo<T> {
   a: u64,
   b: bool,
   c: std::marker::PhantomData<T>
}

Attributes

  • #[encode_as_type(crate_path = "::path::to::scale_encode")]: By default, the macro expects scale_encode to be a top level dependency, available as ::scale_encode. If this is not the case, you can provide the crate path here.
  • #[encode_as_type(trait_bounds = "T: Foo, U::Input: EncodeAsType")]: By default, for each generate type parameter, the macro will add trait bounds such that these type parameters must implement EncodeAsType too. You can override this behaviour and provide your own trait bounds instead using this option.