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