From fb3959d3455c44c64a8591aba20b204023a11200 Mon Sep 17 00:00:00 2001 From: Omar Abdulla Date: Mon, 18 Aug 2025 09:41:25 +0300 Subject: [PATCH] Allow for auto display impl in declare wrapper type macro --- .../common/src/macros/define_wrapper_type.rs | 27 +++++++++++++++++-- crates/format/src/case.rs | 8 +----- crates/format/src/input.rs | 4 +-- crates/format/src/metadata.rs | 4 +-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/crates/common/src/macros/define_wrapper_type.rs b/crates/common/src/macros/define_wrapper_type.rs index 7eb28bc..2196595 100644 --- a/crates/common/src/macros/define_wrapper_type.rs +++ b/crates/common/src/macros/define_wrapper_type.rs @@ -1,3 +1,14 @@ +#[macro_export] +macro_rules! impl_for_wrapper { + (Display, $ident: ident) => { + impl std::fmt::Display for $ident { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } + } + }; +} + /// Defines wrappers around types. /// /// For example, the macro invocation seen below: @@ -42,7 +53,13 @@ macro_rules! define_wrapper_type { ( $(#[$meta: meta])* - $vis:vis struct $ident: ident($ty: ty); + $vis:vis struct $ident: ident($ty: ty) + + $( + impl $($trait_ident: ident),* + )? + + ; ) => { $(#[$meta])* $vis struct $ident($ty); @@ -98,9 +115,15 @@ macro_rules! define_wrapper_type { value.0 } } + + $( + $( + $crate::macros::impl_for_wrapper!($trait_ident, $ident); + )* + )? }; } /// Technically not needed but this allows for the macro to be found in the `macros` module of the /// crate in addition to being found in the root of the crate. -pub use define_wrapper_type; +pub use {define_wrapper_type, impl_for_wrapper}; diff --git a/crates/format/src/case.rs b/crates/format/src/case.rs index 1968ff4..2ef9ead 100644 --- a/crates/format/src/case.rs +++ b/crates/format/src/case.rs @@ -72,11 +72,5 @@ impl Case { define_wrapper_type!( /// A wrapper type for the index of test cases found in metadata file. #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct CaseIdx(usize); + pub struct CaseIdx(usize) impl Display; ); - -impl std::fmt::Display for CaseIdx { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.0) - } -} diff --git a/crates/format/src/input.rs b/crates/format/src/input.rs index 36899b0..afc6845 100644 --- a/crates/format/src/input.rs +++ b/crates/format/src/input.rs @@ -188,7 +188,7 @@ define_wrapper_type! { /// This represents an item in the [`Calldata::Compound`] variant. #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(transparent)] - pub struct CalldataItem(String); + pub struct CalldataItem(String) impl Display; } #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] @@ -233,7 +233,7 @@ pub enum Method { define_wrapper_type!( #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct EtherValue(U256); + pub struct EtherValue(U256) impl Display; ); #[derive(Clone, Debug, Default, Serialize, Deserialize, Eq, PartialEq)] diff --git a/crates/format/src/metadata.rs b/crates/format/src/metadata.rs index 5e6e07d..c09993c 100644 --- a/crates/format/src/metadata.rs +++ b/crates/format/src/metadata.rs @@ -266,7 +266,7 @@ define_wrapper_type!( Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, )] #[serde(transparent)] - pub struct ContractInstance(String); + pub struct ContractInstance(String) impl Display; ); define_wrapper_type!( @@ -277,7 +277,7 @@ define_wrapper_type!( Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, )] #[serde(transparent)] - pub struct ContractIdent(String); + pub struct ContractIdent(String) impl Display; ); /// Represents an identifier used for contracts.