paritytech / paritytech/parity-scale-codec

Allow C-like enum with a repr other than u8 to be Encode/Decode

Open
#255 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
287
Forks
103
Avg merge
6d 13h
Merged PRs (30d)
1

Description

Hi, currently this code cannot compile:

#[derive(Clone, Copy, Debug, Decode, Encode, PartialEq)]
#[repr(u16)]
pub enum Status {
    Foo = 100,
    Bar = 200,
    Baz = 300,
}

Error message:

error: literal out of range for `u8`
  --> frame/foo/src/lib.rs
   |
   |     Baz = 300,
   |           ^^^
   |
   = note: the literal `300` does not fit into the type `u8` whose range is `0..=255`

It's because, as per the documentation,

The variable is encoded with one byte for the variant

but is there a reason to make this rule stands for the C-like enum?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names the Encode and Decode derives and links their enum documentation, but it provides no implementation file or test path. Start by locating the derive handling for C-like enums and determine how repr types are currently interpreted. Done should define and test support for repr(u16) and other non-u8 representations without leaving the encoding behavior ambiguous.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.