From 34dd95f9098c41497243c60527606e8764e71716 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 12 Jul 2024 13:52:01 +1000 Subject: [PATCH] Debug Midstate forwards Done in preparation for adding a `length` field to `Midstate` and also in preparation for removing the `Display` implementation (will be justified in the patch that does it). Currently in the `Debug` impl of `Midstate` we are calling through to `Display` using the alternate form of printing, we can use `as_hex` to achieve almost the same thing. Note that in `LowerHex` we use the `fmt_hex_exact` macro that allows us to reverse the iterator however when we later attempt to use `f.debug_struct` we cannot use the macro. Elect to change the current behaviour to `Debug` forwards, shown by the change to the regression test. --- hashes/src/sha256.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index bbc2d27a9..2057fa057 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -10,6 +10,8 @@ use core::ops::Index; use core::slice::SliceIndex; use core::{cmp, convert, fmt}; +use hex::DisplayHex; + #[cfg(doc)] use crate::sha256t; use crate::{sha256d, FromSliceError, HashEngine as _}; @@ -195,7 +197,7 @@ impl fmt::Display for Midstate { } impl fmt::Debug for Midstate { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:#}", self) } + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "0x{}", self.0.as_hex()) } } impl convert::AsRef<[u8]> for Midstate { @@ -1019,7 +1021,7 @@ mod tests { #[test] fn regression_midstate_debug_format() { - let want = "0xc9f0c6ddea3eb37d606e63476c93f3d389500fc3f2cab338396c117ce6e4e09c"; + let want = "0x9ce0e4e67c116c3938b3caf2c30f5089d3f3936c47636e607db33eeaddc6f0c9"; let got = format!("{:?}", TAP_LEAF_MIDSTATE); assert_eq!(got, want); }