From cb35c86addf8801f7b457a05b671efc534e0ab7e 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 e134706..bba5d13 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 3350c1d..74c4ef0 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] = []; });