ethnum/
lib.rs

1//! This crate implements 256-bit integer types.
2//!
3//! The implementation tries to follow as closely as possible to primitive
4//! integer types, and should implement all the common methods and traits as the
5//! primitive integer types.
6
7#![deny(missing_docs)]
8#![no_std]
9
10#[cfg(test)]
11extern crate alloc;
12
13#[macro_use]
14mod macros {
15    #[macro_use]
16    pub mod cmp;
17    #[macro_use]
18    pub mod fmt;
19    #[macro_use]
20    pub mod iter;
21    #[macro_use]
22    pub mod ops;
23    #[macro_use]
24    pub mod parse;
25}
26
27mod error;
28mod fmt;
29mod int;
30pub mod intrinsics;
31mod parse;
32#[cfg(feature = "serde")]
33pub mod serde;
34mod uint;
35
36/// Macro for 256-bit signed integer literal.
37///
38/// # Examples
39///
40/// Basic usage:
41///
42/// ```
43/// # use ethnum::{int, I256};
44/// assert_eq!(
45///     int!(
46///         "-57896044618658097711785492504343953926634992332820282019728792003956564819968"
47///     ),
48///     I256::MIN,
49/// );
50/// ```
51///
52/// Additionally, this macro accepts `0b` for binary, `0o` for octal, and `0x`
53/// for hexadecimal literals. Using `_` for spacing is also permitted.
54///
55/// ```
56/// # use ethnum::{int, I256};
57/// assert_eq!(
58///     int!(
59///         "0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_ffff
60///            ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff"
61///     ),
62///     I256::MAX,
63/// );
64/// assert_eq!(int!("0b101010"), 42);
65/// assert_eq!(int!("-0o52"), -42);
66/// ```
67#[macro_export]
68macro_rules! int {
69    ($integer:literal) => {{
70        const VALUE: $crate::I256 = $crate::I256::const_from_str_prefixed($integer);
71        VALUE
72    }};
73}
74
75/// Macro for 256-bit unsigned integer literal.
76///
77/// # Examples
78///
79/// Basic usage:
80///
81/// ```
82/// # use ethnum::{uint, U256};
83/// assert_eq!(
84///     uint!(
85///         "115792089237316195423570985008687907852837564279074904382605163141518161494337"
86///     ),
87///     U256::from_words(
88///         0xfffffffffffffffffffffffffffffffe,
89///         0xbaaedce6af48a03bbfd25e8cd0364141,
90///     ),
91/// );
92/// ```
93///
94/// Additionally, this macro accepts `0b` for binary, `0o` for octal, and `0x`
95/// for hexadecimal literals. Using `_` for spacing is also permitted.
96///
97/// ```
98/// # use ethnum::{uint, U256};
99/// assert_eq!(
100///     uint!(
101///         "0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff
102///            ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff"
103///     ),
104///     U256::MAX,
105/// );
106/// assert_eq!(uint!("0b101010"), 42);
107/// assert_eq!(uint!("0o52"), 42);
108/// ```
109#[macro_export]
110macro_rules! uint {
111    ($integer:literal) => {{
112        const VALUE: $crate::U256 = $crate::U256::const_from_str_prefixed($integer);
113        VALUE
114    }};
115}
116
117/// Convenience re-export of 256-integer types and as- conversion traits.
118pub mod prelude {
119    pub use crate::{AsI256, AsU256, I256, U256};
120}
121
122pub use crate::{
123    int::{AsI256, I256},
124    uint::{AsU256, U256},
125};
126
127/// A 256-bit signed integer type.
128#[allow(non_camel_case_types)]
129pub type i256 = I256;
130
131/// A 256-bit unsigned integer type.
132#[allow(non_camel_case_types)]
133pub type u256 = U256;