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

Rust generated code does not support enum 'bitfields' #158

Closed
emily33901 opened this issue Oct 6, 2021 · 1 comment
Closed

Rust generated code does not support enum 'bitfields' #158

emily33901 opened this issue Oct 6, 2021 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@emily33901
Copy link

Describe the bug

Rust runtime cannot serialize or deserialize 'bitfields' (flags, bitflags).

To Reproduce

schema:

enum Flags {
    None = 0;
    A = 1;
    B = 2;
    C = 4;
    D = 8;
}
  1. You cannot serialize a combination of these 'flags' (Flags::A | Flags::B in other languages) (simply not a thing in Rust due to enums), you cannot cast a u32 into a Flags (non-primative)
  2. Deserializing calls the enums try_into which expects distinct values:
impl ::core::convert::TryFrom<u32> for Flags {
    type Error = ::bebop::DeserializeError;

    fn try_from(value: u32) -> ::bebop::DeResult<Self> {
        match value {
            0 => Ok(Flags::None),
            1 => Ok(Flags::A),
            2 => Ok(Flags::B),
            4 => Ok(Flags::C),
            8 => Ok(Flags::D),
            d => Err(::bebop::DeserializeError::InvalidEnumDiscriminator(d)),
        }
    }
}

Expected behavior

Be able to serialize + deserialize 'bitfields' in some way.

Imo distinct enum values should either be enforced or expected for 'regular enums', 'bitfields' can then be denoted with an 'attribute' (Similar to C# [Flags]) to allow bitfields which the code generators can then use.

Bebop info:

  • 2.3.0
  • Rust

Desktop (please complete the following information):

  • OS: Windows
  • Version 10 19043.1237
@emily33901 emily33901 added the bug Something isn't working label Oct 6, 2021
@andrewmd5
Copy link
Contributor

andrewmd5 commented Oct 6, 2021

This will require changes to the schema language. Something like this:

[flags]
enum FileAccess {
  Unknown = 0;
  Read = 1 << 1;
  Write = 1 << 2;
  ReadWrite = Read | Write;
}

Overall not a huge change. Code generators will just need to be updated to look for the flag and handle it however a language sees fit.

@lynn lynn self-assigned this Dec 7, 2021
@lynn lynn closed this as completed in 6833543 Dec 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants