Derive Macro parity_scale_codec_derive::Encode[][src]

#[derive(Encode)]
{
    // Attributes available to this derive:
    #[codec]
}

Derive parity_scale_codec::Encode and parity_scale_codec::EncodeLike for struct and enum.

Struct

A struct is encoded by encoding each of its fields successively.

Fields can have some attributes:

#[derive(Encode)]
struct StructType {
	#[codec(skip)]
	a: u32,
	#[codec(compact)]
	b: u32,
	#[codec(encoded_as = "<u32 as HasCompact>::Type")]
	c: u32,
}

Enum

The variable is encoded with one byte for the variant and then the variant struct encoding. The variant number is:

variant attributes:

field attributes: same as struct fields attributes.

#[derive(Encode)]
enum EnumType {
	#[codec(index = "15")]
	A,
	#[codec(skip)]
	B,
	C = 3,
	D,
}

assert_eq!(EnumType::A.encode(), vec![15]);
assert_eq!(EnumType::B.encode(), vec![]);
assert_eq!(EnumType::C.encode(), vec![3]);
assert_eq!(EnumType::D.encode(), vec![2]);