stratus/eth/primitives/
nonce.rsuse anyhow::anyhow;
use display_json::DebugAsJson;
use ethereum_types::U256;
use ethereum_types::U64;
use fake::Dummy;
use fake::Faker;
use crate::gen_newtype_from;
use crate::gen_newtype_try_from;
#[derive(DebugAsJson, derive_more::Display, Clone, Copy, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Nonce(U64);
impl Nonce {
pub const ZERO: Nonce = Nonce(U64::zero());
pub fn next_nonce(&self) -> Self {
Self(self.0 + 1)
}
}
impl Dummy<Faker> for Nonce {
fn dummy_with_rng<R: rand_core::RngCore + ?Sized>(_: &Faker, rng: &mut R) -> Self {
rng.next_u64().into()
}
}
gen_newtype_from!(self = Nonce, other = u8, u16, u32, u64);
gen_newtype_try_from!(self = Nonce, other = i32);
impl TryFrom<U256> for Nonce {
type Error = anyhow::Error;
fn try_from(value: U256) -> Result<Self, Self::Error> {
Ok(Nonce(u64::try_from(value).map_err(|err| anyhow!(err))?.into()))
}
}
impl From<Nonce> for u64 {
fn from(value: Nonce) -> Self {
value.0.as_u64()
}
}
impl From<Nonce> for U256 {
fn from(value: Nonce) -> Self {
U256::from(value.0.as_u64())
}
}