#[derive(MaxEncodedLen)]
{
    // Attributes available to this derive:
    #[max_encoded_len_mod]
}
Expand description

Derive macro for MaxEncodedLen.

Examples

#[derive(Encode, MaxEncodedLen)]
struct Example;
#[derive(Encode, MaxEncodedLen)]
struct TupleStruct(u8, u32);

assert_eq!(TupleStruct::max_encoded_len(), u8::max_encoded_len() + u32::max_encoded_len());
#[derive(Encode, MaxEncodedLen)]
enum GenericEnum<T> {
	A,
	B(T),
}

assert_eq!(GenericEnum::<u8>::max_encoded_len(), u8::max_encoded_len() + u8::max_encoded_len());
assert_eq!(GenericEnum::<u128>::max_encoded_len(), u8::max_encoded_len() + u128::max_encoded_len());

Within other macros

Sometimes the MaxEncodedLen trait and macro are used within another macro, and it can’t be guaranteed that the parity_scale_codec module is available at the call site. In that case, the macro should reexport the parity_scale_codec module and specify the path to the reexport:

pub use parity_scale_codec as codec;

#[derive(Encode, MaxEncodedLen)]
#[codec(crate = $crate::codec)]
struct Example;

Derive parity_scale_codec::MaxEncodedLen for struct and enum.

Top level attribute

By default the macro will try to bound the types needed to implement MaxEncodedLen, but the bounds can be specified manually with the top level attribute:

#[codec(mel_bound(T: MaxEncodedLen))]