Skip to main content

ic_db/
u256.rs

1//! Database-owned unsigned 256-bit value. Ethnum supplies arithmetic and hex Serde.
2use ethnum::U256 as Uint;
3use std::{fmt, ops, str::FromStr};
4
5/// Unsigned 256-bit integer, encoded as a hexadecimal quantity by Serde.
6#[repr(transparent)]
7#[derive(
8    Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize,
9)]
10#[serde(transparent)]
11pub struct U256(Uint);
12
13impl U256 {
14    /// Smallest representable value.
15    pub const MIN: Self = Self(Uint::MIN);
16    /// Zero.
17    pub const ZERO: Self = Self(Uint::ZERO);
18    /// One.
19    pub const ONE: Self = Self(Uint::ONE);
20    /// Largest representable value.
21    pub const MAX: Self = Self(Uint::MAX);
22
23    /// Widen an unsigned 128-bit integer.
24    #[must_use]
25    pub const fn new(value: u128) -> Self {
26        Self(Uint::new(value))
27    }
28
29    /// Return the high and low 128-bit words.
30    #[must_use]
31    pub const fn into_words(self) -> (u128, u128) {
32        self.0.into_words()
33    }
34
35    /// Construct from high and low 128-bit words.
36    #[must_use]
37    pub const fn from_words(high: u128, low: u128) -> Self {
38        Self(Uint::from_words(high, low))
39    }
40
41    /// Construct from exactly 32 big-endian bytes.
42    #[must_use]
43    pub fn from_be_bytes(bytes: [u8; 32]) -> Self {
44        Self(Uint::from_be_bytes(bytes))
45    }
46
47    /// Return exactly 32 big-endian bytes.
48    #[must_use]
49    pub fn to_be_bytes(self) -> [u8; 32] {
50        self.0.to_be_bytes()
51    }
52
53    /// Add two values, returning None on overflow.
54    #[must_use]
55    pub fn checked_add(self, rhs: Self) -> Option<Self> {
56        self.0.checked_add(rhs.0).map(Self)
57    }
58}
59
60impl From<U256> for Uint {
61    fn from(value: U256) -> Self {
62        value.0
63    }
64}
65
66macro_rules! from_unsigned {
67    ($($ty:ty),+) => {$(
68        impl From<$ty> for U256 {
69            fn from(value: $ty) -> Self { Self(Uint::from(value)) }
70        }
71    )+};
72}
73from_unsigned!(Uint, u8, u16, u32, u64, u128, bool);
74
75impl FromStr for U256 {
76    type Err = <Uint as FromStr>::Err;
77    fn from_str(value: &str) -> Result<Self, Self::Err> {
78        value.parse().map(Self)
79    }
80}
81
82macro_rules! format_uint {
83    ($($trait:ident),+) => {$(
84        impl fmt::$trait for U256 {
85            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86                fmt::$trait::fmt(&self.0, f)
87            }
88        }
89    )+};
90}
91format_uint!(Debug, Display, LowerHex, UpperHex, Binary, Octal);
92
93macro_rules! arithmetic {
94    ($($trait:ident::$method:ident),+) => {$(
95        impl ops::$trait for U256 {
96            type Output = Self;
97            fn $method(self, rhs: Self) -> Self { Self(self.0.$method(rhs.0)) }
98        }
99        impl ops::$trait<u128> for U256 {
100            type Output = Self;
101            fn $method(self, rhs: u128) -> Self { Self(self.0.$method(rhs)) }
102        }
103    )+};
104}
105arithmetic!(Add::add, Sub::sub, Mul::mul, Div::div, Rem::rem);
106
107impl ops::Shl<u32> for U256 {
108    type Output = Self;
109    fn shl(self, shift: u32) -> Self {
110        Self(self.0 << shift)
111    }
112}
113
114impl ops::Shr<u32> for U256 {
115    type Output = Self;
116    fn shr(self, shift: u32) -> Self {
117        Self(self.0 >> shift)
118    }
119}