Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum discriminant is not serialized when specified #137

Closed
encody opened this issue Apr 12, 2023 · 1 comment · Fixed by #138
Closed

Enum discriminant is not serialized when specified #137

encody opened this issue Apr 12, 2023 · 1 comment · Fixed by #138

Comments

@encody
Copy link
Contributor

encody commented Apr 12, 2023

The specification says that for serializing enums, the pseudocode procedure is as follows:

Suppose X is the number of the variant that the enum takes.
repr(X as u8)
repr(x.X as fieldsX)

Therefore, I would expect the following code to work:

#[derive(BorshSerialize)]
enum MyEnum {
    A = 20,
}

assert_eq!(MyEnum::A as u8, 20);
assert_eq!(MyEnum::A.try_to_vec().unwrap(), vec![20]);

However, it fails:

running 1 test
thread 'x' panicked at 'assertion failed: `(left == right)`
  left: `[0]`,
 right: `[20]`', src/lib.rs:10:5

It is serialized the same as if the discriminant were not specified:

#[derive(BorshSerialize)]
enum MyEnum2 {
    A,
}

assert_eq!(
    MyEnum2::A.try_to_vec().unwrap(),
    MyEnum::A.try_to_vec().unwrap(),
);
@frol
Copy link
Collaborator

frol commented Apr 12, 2023

@encody The specification denotes the type of the field, not the fact that as u8 conversion will be used. That said, it might be worth considering adding this feature support. I am happy to review a PR if anyone will submit one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants