From 0d2949024c9061ed935ae93fd64b70c5e0b61c3d Mon Sep 17 00:00:00 2001 From: Erin Date: Sat, 19 Aug 2023 23:57:48 +0200 Subject: [PATCH] updated macro --- hbvm/src/mem/mod.rs | 4 ++-- hbvm/src/utils.rs | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/hbvm/src/mem/mod.rs b/hbvm/src/mem/mod.rs index e134706a..bba5d134 100644 --- a/hbvm/src/mem/mod.rs +++ b/hbvm/src/mem/mod.rs @@ -69,8 +69,8 @@ pub enum MemoryAccessReason { } impl_display!(for MemoryAccessReason => match { - Self::Load => "Load"; - Self::Store => "Store"; + Self::Load => const "Load"; + Self::Store => const "Store"; }); impl From for crate::VmRunError { diff --git a/hbvm/src/utils.rs b/hbvm/src/utils.rs index 3350c1da..74c4ef09 100644 --- a/hbvm/src/utils.rs +++ b/hbvm/src/utils.rs @@ -17,13 +17,13 @@ macro_rules! impl_display { }; (for $ty:ty => match {$( - $bind:pat => $fmt:literal $(,$($param:expr),* $(,)?)?; + $bind:pat => $($const:ident)? $fmt:literal $(,$($params:tt)*)?; )*}) => { impl ::core::fmt::Display for $ty { fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { match self { $( - $bind => write!(f, $fmt, $($($param),*)?) + $bind => $crate::utils::private::impl_display_match_fragment!($($const,)? f, $fmt $(, $($params)*)?) ),* } } @@ -31,6 +31,21 @@ macro_rules! impl_display { } } +#[doc(hidden)] +pub(crate) mod private { + macro_rules! impl_display_match_fragment { + (const, $f:expr, $lit:literal) => { + $f.write_str($lit) + }; + + ($f:expr, $fmt:literal $(, $($params:tt)*)?) => { + write!($f, $fmt, $($($params)*)?) + }; + } + + pub(crate) use impl_display_match_fragment; +} + macro_rules! static_assert_eq(($l:expr, $r:expr $(,)?) => { const _: [(); ($l != $r) as usize] = []; });