2 stable releases

1.0.3 Jun 13, 2023

#18 in #uint

Download history 155/week @ 2024-03-14 221/week @ 2024-03-21 108/week @ 2024-03-28 133/week @ 2024-04-04 237/week @ 2024-04-11 239/week @ 2024-04-18 439/week @ 2024-04-25 715/week @ 2024-05-02 787/week @ 2024-05-09 679/week @ 2024-05-16 778/week @ 2024-05-23 968/week @ 2024-05-30 1079/week @ 2024-06-06 1253/week @ 2024-06-13 1036/week @ 2024-06-20 799/week @ 2024-06-27

4,294 downloads per month
Used in 2 crates (via ruint2)

Custom license

13KB
210 lines

The uint! macro for Uint and Bits literals

Within the uint! macro arguments, you can write Uint and Bits literals using the same syntax as Rust integer literals, but using a capital U or B suffix respectively. Note that there is ambiguity for hexadecimals with a B suffix, to lessen the impact an underscore is required in this case.

To use it simply import it in scope:

use ruint2::uint;

Now constants can be created in decimal, hex, binary and even octal:

# use ruint2::uint;
let avogadro = uint!(602_214_076_000_000_000_000_000_U256);
let cow_key = uint!(0xee79b5f6e221356af78cf4c36f4f7885a11b67dfcc81c34d80249947330c0f82_U256);
let bender = uint!(0b1010011010_U10);

The uint! macro recurses through the parse tree, so the above can equivalently be written

# use ruint2::uint;
uint!{
let avogadro = 602_214_076_000_000_000_000_000_U256;
let cow_key = 0xee79b5f6e221356af78cf4c36f4f7885a11b67dfcc81c34d80249947330c0f82_U256;
let bender = 0b1010011010_U10;
}

This latter form is particularly useful for lookup tables:

# use ruint2::{Uint, uint};
const PRIMES: [Uint<128, 2>; 3] = uint!([
    170141183460469231731687303715884105757_U128,
    170141183460469231731687303715884105773_U128,
    170141183460469231731687303715884105793_U128,
]);

The macro will throw a compile time error if you try to create a constant that does not fit the type:

# use ruint2::uint;
# uint!{
let sparta = 300_U8;
# }
error: Value too large for Uint<8>: 300
 --> src/example.rs:1:14
  |
1 | let sparta = 300_U8;
  |              ^^^^^^

References

No runtime deps