stratus/eth/primitives/
gas.rs1use alloy_primitives::U64;
2use display_json::DebugAsJson;
3use fake::Dummy;
4use fake::Faker;
5
6use crate::ext::RuintExt;
7
8#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
9#[serde(transparent)]
10pub struct Gas(U64);
11
12impl Gas {
13 pub const ZERO: Gas = Gas(U64::ZERO);
14 pub const MAX: Gas = Gas(U64::MAX);
15
16 pub fn as_u64(&self) -> u64 {
17 self.0.as_u64()
18 }
19}
20
21impl Dummy<Faker> for Gas {
22 fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
23 rng.next_u64().into()
24 }
25}
26
27impl From<u8> for Gas {
32 fn from(value: u8) -> Self {
33 Self(U64::from(value))
34 }
35}
36
37impl From<u16> for Gas {
38 fn from(value: u16) -> Self {
39 Self(U64::from(value))
40 }
41}
42
43impl From<u32> for Gas {
44 fn from(value: u32) -> Self {
45 Self(U64::from(value))
46 }
47}
48
49impl From<u64> for Gas {
50 fn from(value: u64) -> Self {
51 Self(U64::from(value))
52 }
53}
54
55impl TryFrom<i32> for Gas {
56 type Error = anyhow::Error;
57
58 fn try_from(value: i32) -> Result<Self, Self::Error> {
59 if value < 0 {
60 return Err(anyhow::anyhow!("Gas cannot be negative"));
61 }
62 Ok(Self(U64::from(value as u32)))
63 }
64}
65
66impl From<Gas> for u64 {
71 fn from(value: Gas) -> Self {
72 value.as_u64()
73 }
74}