#uint #macro

macro ruint-macro

The uint! macro for Uint literals

7 releases (3 stable)

1.0.2 Nov 1, 2022
1.0.1 Oct 24, 2022
1.0.0 Jun 4, 2022
0.2.1 May 18, 2022
0.1.1 May 15, 2022

#18 in #uint

Download history 119/week @ 2022-11-28 189/week @ 2022-12-05 312/week @ 2022-12-12 1388/week @ 2022-12-19 711/week @ 2022-12-26 849/week @ 2023-01-02 687/week @ 2023-01-09 1188/week @ 2023-01-16 1654/week @ 2023-01-23 1469/week @ 2023-01-30 1938/week @ 2023-02-06 2953/week @ 2023-02-13 1793/week @ 2023-02-20 1415/week @ 2023-02-27 1780/week @ 2023-03-06 1662/week @ 2023-03-13

6,772 downloads per month
Used in 8 crates (via ruint)

Custom license

10KB
166 lines

The uint! macro for Uint literals

The above can also be written using the [uint!] macro. Within the macro arguments, you can write Uint literals using the same syntax as Rust integer literals, but using a capital U in the suffix instead of lowercase.

To use it simply import it in scope:

use ruint::uint;

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

# use ruint::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 ruint::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 ruint::{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 ruint::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