isaacholt100 / bnum

Arbitrary, fixed size numeric types that extend the functionality of primitive numeric types in Rust.
https://crates.io/crates/bnum
Other
73 stars 10 forks source link

Implement a macro to allow compile-time parsing of integer literals #18

Open isaacholt100 opened 1 year ago

isaacholt100 commented 1 year ago

As mentioned in #17, it would be helpful to have a way of initialising integers at compile from a literal. Although the from_str method on the FromStr implementation on bnum integers can be made into a const function (although it would have to be a regular method rather than a trait method currently), this would only allow parsing of decimal literal values. Rust integers can be initialised from a decimal, octal, hexadecimal or binary literal, so it would be good to allow the compile-time parsing of bnum integers from the same bases.

The macro would most likely have to be a proc macro which would mean building a separate crate with this functionality.

The macro syntax could look something like this:

int!(0x64e0453943049583045948340fa3eu256);
int!(-349572394579283457934958374934, I256);

The suffix of uBITS or iBITS would be mandatory, unless an optional second argument was passed, indicating the type of the desired output.