chiark / gitweb /
utils: Break out format_by_fmt_hex
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 29 May 2021 21:21:32 +0000 (22:21 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 30 May 2021 12:44:07 +0000 (13:44 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bundles.rs
src/prelude.rs
src/utils.rs

index 7c85d95f71e1fd2fa202e48569e8d1dc02e6bc34..485068373e6098a696249aa5c1dbde271b77aee6 100644 (file)
@@ -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) {
index 277ae8980c2d168b46a5eb2108ef374f4261d235..fbb3c86bcb0173e513c53da9a53d8a2cbec52337 100644 (file)
@@ -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};
index 0573f389a6a90ee36d240b014e1f178ac389b675..75500470c42e9406546571ff1f153fd0ca3e481a 100644 (file)
@@ -642,3 +642,21 @@ impl<I,T> IndexVec<I,T> 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)+)?
+      }
+    }
+  }
+}