From 69f44d9301c54168e9eaaf046db3d478e6739173 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Fri, 7 Jan 2022 10:31:15 +1100 Subject: [PATCH] Manually implement Debug for SerializedSignature Currently we have an implementation of `Debug` (also used by `Display`) for `Signature` that first converts the sig to a `SerializedSignature` then prints it as hex. We would like to have an implementation of `Debug` for `SerializedSignature`, this cannot be derived because of the `data: [u8; field]`. We can manually implement `Debug` for `SerializedSignature` exactly as it is currently done for `Signature` and call this new implementation from `Signature::fmt()`. This code path is already tested in `lib.rs` in the test function `signature_display`. --- src/ecdsa/mod.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ecdsa/mod.rs b/src/ecdsa/mod.rs index cedd844..52b276c 100644 --- a/src/ecdsa/mod.rs +++ b/src/ecdsa/mod.rs @@ -32,7 +32,19 @@ impl fmt::Debug for Signature { impl fmt::Display for Signature { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let sig = self.serialize_der(); - for v in sig.iter() { + sig.fmt(f) + } +} + +impl fmt::Debug for SerializedSignature { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for SerializedSignature { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + for v in self.data.iter().take(self.len) { write!(f, "{:02x}", v)?; } Ok(())