diff --git a/CHANGELOG.md b/CHANGELOG.md index 30ed61aa..f015c10b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ - [#1535](https://github.com/gakonst/ethers-rs/pull/1535) Add support to Aurora and Aurora testnet networks. - [#1632](https://github.com/gakonst/ethers-rs/pull/1632) Re-export `H32` from `ethabi`. - [#1634](https://github.com/gakonst/ethers-rs/pull/1634) Derive missing `Clone`, `Copy` and `Debug` impls in ethers-etherscan. +- Bytes debug format now displays hex literals [#1658](https://github.com/gakonst/ethers-rs/pull/1658) ## ethers-contract-abigen diff --git a/ethers-core/src/types/bytes.rs b/ethers-core/src/types/bytes.rs index e6cbca1a..4b1ffb64 100644 --- a/ethers-core/src/types/bytes.rs +++ b/ethers-core/src/types/bytes.rs @@ -10,7 +10,7 @@ use std::{ use thiserror::Error; /// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings -#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd)] +#[derive(Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd)] pub struct Bytes( #[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")] pub bytes::Bytes, @@ -20,6 +20,12 @@ fn bytes_to_hex(b: &Bytes) -> String { hex::encode(b.0.as_ref()) } +impl Debug for Bytes { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + write!(f, "Bytes(0x{})", bytes_to_hex(self)) + } +} + impl Display for Bytes { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!(f, "0x{}", bytes_to_hex(self)) @@ -209,4 +215,11 @@ mod tests { let b = b.unwrap(); assert_eq!(b.as_ref(), hex::decode("1213").unwrap()); } + + #[test] + fn test_debug_formatting() { + let b = Bytes::from(vec![1, 35, 69, 103, 137, 171, 205, 239]); + assert_eq!(format!("{:?}", b), "Bytes(0x0123456789abcdef)"); + assert_eq!(format!("{:#?}", b), "Bytes(0x0123456789abcdef)"); + } }