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

Enums Size Option(Feature Request) #235

Closed
Dimkar3000 opened this issue May 23, 2018 · 5 comments
Closed

Enums Size Option(Feature Request) #235

Dimkar3000 opened this issue May 23, 2018 · 5 comments
Milestone

Comments

@Dimkar3000
Copy link

@Dimkar3000 Dimkar3000 commented May 23, 2018

It would be really convenient if I was able to configure the size of the Enum types index. I am currently writing an application that focus maximum compression of the data. The 4 byte size of the Enum size is more than the data size itself which in return makes me unable to use them. I think even at compile time or with an annotation I should be able to set the size of the Enum.

In my project no enum ever goes beyond the 512 discrete values barrier, so this is my suggestion:

  • Make the size of enums or any object that gets serialized with an variable size configurable in compile time with an optional feature
  • or create an annotation that allows me to set the size of variable length structs for any sub-type of a struct like this:
    #derive(BINCODE_BYTE_SIZE)
    struct Points {
         small_vac:Vec<u8> // I only allow 100 values in here
    }

@TyOverby TyOverby added this to the post-1.0 milestone May 23, 2018
@TyOverby
Copy link
Collaborator

@TyOverby TyOverby commented May 23, 2018

Yep, this is something that I've also wanted for a long time!

I don't think that you could add it as an annotation to the struct, but it could certainly be a compile-time parameter to the bincode

@Dimkar3000
Copy link
Author

@Dimkar3000 Dimkar3000 commented May 25, 2018

This could be extended to any std type that get serialized as (size,[u8]) as it is also really common for strings that contains usernames, passwords, dates, to be really short. The reason a mentioned a trait annotation is to allow certain fields to be serialized with a smaller index size and others with a greater one.

If you think it is not viable then at least it should be done for Enums, as a discrete field type.

@sirkibsirkib
Copy link

@sirkibsirkib sirkibsirkib commented Jul 5, 2018

I'd like to second this. Annotating the enum would be amazing! In almost all cases, a u8 would be sufficient

@Kerollmops
Copy link

@Kerollmops Kerollmops commented Nov 24, 2018

Is it possible to know the number of variant of an enum ?

If so it can be possible to choose the adapted integer type to encode the variant number. For example an Option<T> could use an u8 and an enum with more than 256 variants could use an u16 and so on.

The deserialiser knows the enum type that it is decoding therefore it knows the number of variant and so the integer type to decode.

It is a breaking change to do this but it provides a perfect fit between correctness and data compression.

@ZoeyR
Copy link
Collaborator

@ZoeyR ZoeyR commented Apr 16, 2020

Closing in favor of #319

@ZoeyR ZoeyR closed this Apr 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.