bigint/
gas.rs

1//! Gas capped at the maximum number of U256. It is practically
2//! impossible to obtain this number during a block formation.
3
4use super::{M256, U256};
5#[cfg(feature = "string")]
6use hexutil::ParseHexError;
7#[cfg(feature = "rlp")]
8use rlp::{Encodable, Decodable, RlpStream, DecoderError, UntrustedRlp};
9
10#[cfg(feature = "std")] use std::ops::{Add, Sub, Mul, Div, Rem};
11#[cfg(feature = "std")] use std::cmp::Ordering;
12#[cfg(feature = "std")] use std::str::FromStr;
13#[cfg(feature = "std")] use std::fmt;
14
15#[cfg(not(feature = "std"))] use core::ops::{Add, Sub, Mul, Div, Rem};
16#[cfg(not(feature = "std"))] use core::cmp::Ordering;
17#[cfg(not(feature = "std"))] use core::str::FromStr;
18#[cfg(not(feature = "std"))] use core::fmt;
19
20#[derive(Eq, PartialEq, Debug, Copy, Clone, Hash)]
21pub struct Gas(U256);
22
23impl Gas {
24    /// Zero value of Gas,
25    pub fn zero() -> Gas { Gas(U256::zero()) }
26    /// One value of Gas,
27    pub fn one() -> Gas { Gas(U256::one()) }
28    /// Maximum value of Gas,
29    pub fn max_value() -> Gas { Gas(U256::max_value()) }
30    /// Minimum value of Gas,
31    pub fn min_value() -> Gas { Gas(U256::min_value()) }
32    /// Bits required to represent this value.
33    pub fn bits(self) -> usize { self.0.bits() }
34    /// Equals `floor(log2(*))`. This is always an integer.
35    pub fn log2floor(self) -> usize { self.0.log2floor() }
36    /// Conversion to u32 with overflow checking
37    ///
38    /// # Panics
39    ///
40    /// Panics if the number is larger than 2^32.
41    pub fn as_u32(&self) -> u32 {
42        self.0.as_u32()
43    }
44    /// Conversion to u64 with overflow checking
45    ///
46    /// # Panics
47    ///
48    /// Panics if the number is larger than 2^64.
49    pub fn as_u64(&self) -> u64 {
50        self.0.as_u64()
51    }
52    /// Conversion to usize with overflow checking
53    ///
54    /// # Panics
55    ///
56    /// Panics if the number is larger than usize::max_value().
57    pub fn as_usize(&self) -> usize {
58        self.0.as_usize()
59    }
60}
61
62impl Default for Gas { fn default() -> Gas { Gas::zero() } }
63
64#[cfg(feature = "string")]
65impl FromStr for Gas {
66    type Err = ParseHexError;
67
68    fn from_str(s: &str) -> Result<Gas, ParseHexError> {
69        U256::from_str(s).map(|s| Gas(s))
70    }
71}
72
73#[cfg(feature = "rlp")]
74impl Encodable for Gas {
75    fn rlp_append(&self, s: &mut RlpStream) {
76        self.0.rlp_append(s);
77    }
78}
79
80#[cfg(feature = "rlp")]
81impl Decodable for Gas {
82    fn decode(rlp: &UntrustedRlp) -> Result<Self, DecoderError> {
83        Ok(Gas(U256::decode(rlp)?))
84    }
85}
86
87impl From<u64> for Gas { fn from(val: u64) -> Gas { Gas(U256::from(val)) } }
88impl Into<u64> for Gas { fn into(self) -> u64 { self.0.into() } }
89impl From<usize> for Gas { fn from(val: usize) -> Gas { Gas(U256::from(val)) } }
90impl<'a> From<&'a [u8]> for Gas { fn from(val: &'a [u8]) -> Gas { Gas(U256::from(val)) } }
91impl From<bool> for Gas {
92    fn from(val: bool) -> Gas {
93        if val {
94            Gas::one()
95        } else {
96            Gas::zero()
97        }
98    }
99}
100impl From<U256> for Gas { fn from(val: U256) -> Gas { Gas(val) } }
101impl Into<U256> for Gas { fn into(self) -> U256 { self.0 } }
102impl From<M256> for Gas {
103    fn from(val: M256) -> Gas {
104        Gas(val.into())
105    }
106}
107impl Into<M256> for Gas {
108    fn into(self) -> M256 {
109        self.0.into()
110    }
111}
112
113impl Ord for Gas { fn cmp(&self, other: &Gas) -> Ordering { self.0.cmp(&other.0) } }
114impl PartialOrd for Gas {
115    fn partial_cmp(&self, other: &Gas) -> Option<Ordering> {
116        self.0.partial_cmp(&other.0)
117    }
118}
119
120impl Add<Gas> for Gas {
121    type Output = Gas;
122
123    fn add(self, other: Gas) -> Gas {
124        Gas(self.0.saturating_add(other.0))
125    }
126}
127
128impl Sub<Gas> for Gas {
129    type Output = Gas;
130
131    fn sub(self, other: Gas) -> Gas {
132        Gas(self.0 - other.0)
133    }
134}
135
136impl Mul<Gas> for Gas {
137    type Output = Gas;
138
139    fn mul(self, other: Gas) -> Gas {
140        Gas(self.0.saturating_mul(other.0))
141    }
142}
143
144impl Div for Gas {
145    type Output = Gas;
146
147    fn div(self, other: Gas) -> Gas {
148        Gas(self.0 / other.0)
149    }
150}
151
152impl Rem for Gas {
153    type Output = Gas;
154
155    fn rem(self, other: Gas) -> Gas {
156        Gas(self.0 % other.0)
157    }
158}
159
160impl fmt::LowerHex for Gas {
161    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162        write!(f, "{:x}", self.0)
163    }
164}
165
166impl fmt::UpperHex for Gas {
167    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
168        write!(f, "{:X}", self.0)
169    }
170}