1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//! This crate implements 256-bit integer types.
//!
//! The implementation tries to follow as closely as possible to primitive
//! integer types, and should implement all the common methods and traits as the
//! primitive integer types.

#![deny(missing_docs)]
#![no_std]

#[cfg(test)]
extern crate alloc;

#[macro_use]
mod macros {
    #[macro_use]
    pub mod cmp;
    #[macro_use]
    pub mod fmt;
    #[macro_use]
    pub mod iter;
    #[macro_use]
    pub mod ops;
    #[macro_use]
    pub mod parse;
}

mod error;
mod fmt;
mod int;
pub mod intrinsics;
mod parse;
#[cfg(feature = "serde")]
pub mod serde;
mod uint;

/// Macro for 256-bit signed integer literal.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use ethnum::{int, I256};
/// assert_eq!(
///     int!(
///         "-57896044618658097711785492504343953926634992332820282019728792003956564819968"
///     ),
///     I256::MIN,
/// );
/// ```
///
/// Additionally, this macro accepts `0b` for binary, `0o` for octal, and `0x`
/// for hexadecimal literals. Using `_` for spacing is also permitted.
///
/// ```
/// # use ethnum::{int, I256};
/// assert_eq!(
///     int!(
///         "0x7fff_ffff_ffff_ffff_ffff_ffff_ffff_ffff
///            ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff"
///     ),
///     I256::MAX,
/// );
/// assert_eq!(int!("0b101010"), 42);
/// assert_eq!(int!("-0o52"), -42);
/// ```
#[macro_export]
macro_rules! int {
    ($integer:literal) => {{
        const VALUE: $crate::I256 = $crate::I256::const_from_str_prefixed($integer);
        VALUE
    }};
}

/// Macro for 256-bit unsigned integer literal.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// # use ethnum::{uint, U256};
/// assert_eq!(
///     uint!(
///         "115792089237316195423570985008687907852837564279074904382605163141518161494337"
///     ),
///     U256::from_words(
///         0xfffffffffffffffffffffffffffffffe,
///         0xbaaedce6af48a03bbfd25e8cd0364141,
///     ),
/// );
/// ```
///
/// Additionally, this macro accepts `0b` for binary, `0o` for octal, and `0x`
/// for hexadecimal literals. Using `_` for spacing is also permitted.
///
/// ```
/// # use ethnum::{uint, U256};
/// assert_eq!(
///     uint!(
///         "0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff
///            ffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff"
///     ),
///     U256::MAX,
/// );
/// assert_eq!(uint!("0b101010"), 42);
/// assert_eq!(uint!("0o52"), 42);
/// ```
#[macro_export]
macro_rules! uint {
    ($integer:literal) => {{
        const VALUE: $crate::U256 = $crate::U256::const_from_str_prefixed($integer);
        VALUE
    }};
}

/// Convenience re-export of 256-integer types and as- conversion traits.
pub mod prelude {
    pub use crate::{AsI256, AsU256, I256, U256};
}

pub use crate::{
    int::{AsI256, I256},
    uint::{AsU256, U256},
};

/// A 256-bit signed integer type.
#[allow(non_camel_case_types)]
pub type i256 = I256;

/// A 256-bit unsigned integer type.
#[allow(non_camel_case_types)]
pub type u256 = U256;