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

Feature request: bnum compatibility #279

Open
aumetra opened this issue Mar 20, 2024 · 2 comments
Open

Feature request: bnum compatibility #279

aumetra opened this issue Mar 20, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@aumetra
Copy link
Contributor

aumetra commented Mar 20, 2024

Is your feature request related to a problem? Please describe.

In my exploration in adopting ts-rs for TypeScript typegen in a project, I hit the roadblock of needing a bnum implementation.

Describe the solution you'd like

An implementation for the bnum structs would be nice, but there is something to discuss here, which is why I opened an issue here first.


First off, here's a link to the documentation of the crate: https://docs.rs/bnum/latest/bnum/

While crate is made for arithmetic, it is made for arbitrary precision arithmetic.
Now here's the thing, should the type for bnum be expressed as a number, a string, or some binary format.

The serde impl seems to serialize it as an array of limbs, but in most cases you'd probably want to serialize it as a string and parse it radix 10 or 16.

Personally, I'd express the type as string? Or should bigint be chosen here? (not sure if it's arbitrary precision?)

@aumetra aumetra added the enhancement New feature or request label Mar 20, 2024
@NyxCode
Copy link
Collaborator

NyxCode commented Mar 20, 2024

Hey!
If we add an impl for it behind a bnum-impl feature gate, then that impl should be 1:1 compatible with their serde serialization. I think that's the only way that feature would be usefull for the average user.

If you're doing something special for serialization, you can use #[ts(type = "string")] or #[ts(as = "..")].

@NyxCode
Copy link
Collaborator

NyxCode commented Mar 20, 2024

Our impls for large integer types (e.g u128) do currently use bigint, but it was a somewhat painfull tradeoff with them.
Serializing them as number would be serde-compatible by default, but probably wrong in almost all cases.

If I had the chance to go back and change it, I'd probably still chose number though. Then, it's serde-compatible, and when users chose a way of serializing them, they can also change the TS type using #[ts(type = "..")] or #[ts(as = "..")].

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

No branches or pull requests

2 participants