Using scale, an enum can be written like this:

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
pub enum Block {
    #[codec(index = 101)]

The #[codec(index = 101)], the 101 part, represents the u8 value that will be encoded in the serialization to represent the current enum arm. When this enum gets serialized, the first byte of that serialization will have the value 101.

Given that this whole mechanism is built-in into the scale-codec, and given that making assumptions about serialization is wrong to respect encapsulation, is there a way to retrieve that 101 through a method call to the enum?

So I'm hoping for something like:

fn make_block() -> Block {
    // some implementation

fn get_that_version() -> u8 {
    let block = make_block();
    assert_eq!(block.codec_arm_index(), 101u8); // notice 101 is the same from the enum

Is such functionality available in scale?

1 Answer 1


No there is no way to extract these information from a type. I think in your case you should just add a function to Block get_version.

  • The bad thing there is that I have to match inside that function and write the value twice, once in the codec parameter and once inside the get_version function. The codec parameter must be a literal, which means if I ever decide to change the encoding index for any reason (this is not specific to blocks), there will be a consistency problem that's only caught by a test that may or may not exist. I hope it's not so much trouble to add this function to scale. Commented May 18, 2022 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.