Skip to content

Support for Enums with more than 256 variants #45

Description

@ZJaume

Are there any plans on supporting enums larger than u8? I'm currently developing a language identifier that has 238 variants for the language codes and will probably surpass that amount in the future when more languages are added.

Also, I found the error a bit confusing

error: enums with more than 256 variants are not supported
  --> src/main.rs:10:10
   |
10 | pub enum Lang {
   |          ^^^^

because it does not say anything about what is causing the error. So I did not know if it was a limitation from the language, the strum derives or the bitcode derives.

Thank you very much for your library. It was one of the main optimizations I did for my tool and was able to cut down the model loading time more than a half.

Activity

  1. caibear commented on Sep 23, 2024

    @caibear
    Collaborator

    The main reason I limited enums to a 256 variants is because bitcode 0.6 requires calculating a histogram of all the variants of each enum while deserializing. With 256 variants a [usize; 256] can be used to calculate the histogram which is quite fast. For an arbitrary number of variants a HashMap<u32, usize> is required which isn't fast.

  2. finnbear commented on Sep 23, 2024

    @finnbear
    Member

    requires calculating a histogram

    Perhaps an exception can be made for C-style enums, i.e. those that lack fields in their variants, which don't need a histogram.

  3. ZJaume commented on Sep 24, 2024

    @ZJaume
    Author

    Is a [usize; 65536] too much for that histogram?

  4. caibear commented on Sep 24, 2024

    @caibear
    Collaborator

    Is a [usize; 65536] too much for that histogram?

    Yes, because that would require iterating all 65536 elements to see which ones are non zero which is significant overhead.
    This would only be faster than a HashMap<u32, usize> for calculating a histogram of a very large message where the 1 time cost of iteration does not dominate the runtime.

    @finnbear does bring up a good point that histograms are only required non C-style enums so we could support u16 C-style enums easier.

  5. ZJaume commented on Feb 10, 2025

    @ZJaume
    Author

    Has been any progress on supporting u16 C-style enums? I'm not familiar with the bitcode source code, but could try to implement it if it's easy and you point me in the right direction.

    Edit: without serde

  6. finnbear commented on Feb 10, 2025

    @finnbear
    Member

    Has been any progress on supporting u16 C-style enums?

    No, we're not currently working on it.

  7. caibear commented on Feb 10, 2025

    @caibear
    Collaborator

    The next feature we plan to add is the ability to use serde to encode a field within the Encode macro. This provides a solution to many edge cases at once. After that we can revisit this issue and see if it's still necessary.

  8. jstrong-lhava commented on Aug 23, 2025

    @jstrong-lhava

    hello,

    I just came across this when trying to add bitcode to give it a spin, but it would not work for an enum with more than 256 variants, which in this case, stopped me in my tracks. The enum in my case is #[repr(u16)] and a C-style enum, so I was hoping to find some kind of escape hatch that would let me specify to just treat it as a u16 integer for Encode/Decode.

    Thanks for your work on this library! The benchmark results are impressive, eager to try it out soon.

  9. finnbear commented on Aug 23, 2025

    @finnbear
    Member

    Hi @jstrong-lhava; thanks for your comment! This provides motivation for supporting C-style enums with more than 256 variants. Unfortunately, there is no escape hatch other than #[bitcode(skip)] on any un-encodable fields that you don't need to encode.

  10. 448-OG commented on Mar 22, 2026

    @448-OG

    Hi! I came across this problem too. Any progress on getting it #84 merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions