From b928cd91da9a209327d72ed3515fb2ea16c69947 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 29 May 2021 22:21:32 +0100 Subject: [PATCH] utils: Break out format_by_fmt_hex Signed-off-by: Ian Jackson --- src/bundles.rs | 10 +--------- src/prelude.rs | 1 + src/utils.rs | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/bundles.rs b/src/bundles.rs index 7c85d95f..48506837 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -197,15 +197,7 @@ impl BundleSavefile { } } -#[throws(fmt::Error)] -fn fmt_hex(f: &mut Formatter, buf: &[u8]) { - for v in buf { write!(f, "{:02x}", v)?; } -} - -impl Debug for Hash { - #[throws(fmt::Error)] - fn fmt(&self, f: &mut Formatter) { fmt_hex(f, &self.0)? } -} +format_by_fmt_hex!{Debug, for Hash, .0} impl Display for Hash { #[throws(fmt::Error)] fn fmt(&self, f: &mut Formatter) { diff --git a/src/prelude.rs b/src/prelude.rs index 277ae898..fbb3c86b 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -119,6 +119,7 @@ pub use base_misc::*; pub use crate::dbgc; pub use crate::{deref_to_field, deref_to_field_mut}; pub use crate::ensure_eq; +pub use crate::format_by_fmt_hex; pub use crate::matches_doesnot; pub use crate::trace_dbg; pub use crate::{want, wantok, wants, want_let, want_failed_internal}; diff --git a/src/utils.rs b/src/utils.rs index 0573f389..75500470 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -642,3 +642,21 @@ impl IndexVec where I: index_vec::Idx { self.raw.get_or_extend_with(i.index(), f) } } + + +#[throws(fmt::Error)] +pub fn fmt_hex(f: &mut Formatter, buf: &[u8]) { + for v in buf { write!(f, "{:02x}", v)?; } +} + +#[macro_export] +macro_rules! format_by_fmt_hex { + ($trait:ty, for $self:ty, . $($memb:tt)+) => { + impl $trait for $self { + #[throws(fmt::Error)] + fn fmt(&self, f: &mut Formatter) { + fmt_hex(f, &self . $($memb)+)? + } + } + } +} -- 2.30.2