Allow for auto display impl in declare wrapper type macro

This commit is contained in:
Omar Abdulla
2025-08-18 09:41:25 +03:00
parent 84026f9aee
commit fb3959d345
4 changed files with 30 additions and 13 deletions
@@ -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};
+1 -7
View File
@@ -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)
}
}
+2 -2
View File
@@ -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)]
+2 -2
View File
@@ -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.