#literals #u256 #proc-macro #ethereum #compile-time

macro u256-literal

Procedural macro for converting u256 literals to U256 at compile time

8 releases (stable)

1.2.1 Jan 27, 2023
1.2.0 Jul 22, 2022
1.1.2 May 26, 2022
1.1.0 Oct 17, 2021
0.1.0 Jun 17, 2021

#7 in #u256

Download history 633/week @ 2024-07-19 956/week @ 2024-07-26 1000/week @ 2024-08-02 2095/week @ 2024-08-09 2065/week @ 2024-08-16 1224/week @ 2024-08-23 1622/week @ 2024-08-30 1662/week @ 2024-09-06 2145/week @ 2024-09-13 1286/week @ 2024-09-20 906/week @ 2024-09-27 1562/week @ 2024-10-04 1051/week @ 2024-10-11 527/week @ 2024-10-18 656/week @ 2024-10-25 539/week @ 2024-11-01

2,866 downloads per month
Used in 19 crates (2 directly)

MIT/Apache

6KB
58 lines

u256-literal

Description

This crate helps you to declare a constant U256 in an easy way. No need to wrap the integer literal around double quotes.

Usage

use primitive_types::U256;
use u256_literal::u256;

const Q96: U256 = u256!(0x1000000000000000000000000);

// support hex style integer
let x = u256!(0x1234ff);
// support normal style
let y = u256!(987654);
// support float style number
let z = u256!(1.23e18);

License

All crates licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

This crate provides u256! macro for converting u256 literals to actual primitive_types::U256 at compile time. This is helpful when you're trying to define a const U256.

It supports 0x prefix and normal decimals. When the number is too large to fit into U256, it would complain.

This crate assumes you have primitive-types added to your Cargo.toml dependency.

Examples

let x = u256!(123454444444444444444444444444444444444444444);
let y = u256!(0x123456789827823721235568483);
let z = u256!(1e18);
let w = u256!(1.23e18);

Dependencies

~2MB
~42K SLoC