stratus/eth/primitives/
size.rs

1use alloy_primitives::U64;
2use alloy_primitives::U256;
3use anyhow::anyhow;
4use display_json::DebugAsJson;
5use fake::Dummy;
6use fake::Faker;
7
8use crate::ext::RuintExt;
9
10#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11#[serde(transparent)]
12pub struct Size(U64);
13
14impl Dummy<Faker> for Size {
15    fn dummy_with_rng<R: rand::Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
16        Self(U64::random_with(rng))
17    }
18}
19
20// -----------------------------------------------------------------------------
21// Conversions: Other -> Self
22// -----------------------------------------------------------------------------
23
24impl From<u64> for Size {
25    fn from(value: u64) -> Self {
26        Self(U64::from(value))
27    }
28}
29
30impl TryFrom<U256> for Size {
31    type Error = anyhow::Error;
32
33    fn try_from(value: U256) -> Result<Self, Self::Error> {
34        Ok(Size(U64::from(u64::try_from(value).map_err(|err| anyhow!(err))?)))
35    }
36}
37
38// -----------------------------------------------------------------------------
39// Conversions: Self -> Other
40// ----------------------------------------------------------------------------
41impl From<Size> for u64 {
42    fn from(value: Size) -> Self {
43        value.0.as_u64()
44    }
45}
46
47impl From<Size> for U256 {
48    fn from(value: Size) -> Self {
49        U256::from(value.0)
50    }
51}