feat(core): improved debug format for Bytes (#1658)

* feat(core): improved debug format for Bytes

* update changelog
This commit is contained in:
Clifton King 2022-09-04 12:58:06 -05:00 committed by GitHub
parent 6a86d4ff22
commit de90662725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -85,6 +85,7 @@
- [#1535](https://github.com/gakonst/ethers-rs/pull/1535) Add support to Aurora and Aurora testnet networks. - [#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`. - [#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. - [#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 ## ethers-contract-abigen

View File

@ -10,7 +10,7 @@ use std::{
use thiserror::Error; use thiserror::Error;
/// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings /// 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( pub struct Bytes(
#[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")] #[serde(serialize_with = "serialize_bytes", deserialize_with = "deserialize_bytes")]
pub bytes::Bytes, pub bytes::Bytes,
@ -20,6 +20,12 @@ fn bytes_to_hex(b: &Bytes) -> String {
hex::encode(b.0.as_ref()) 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 { impl Display for Bytes {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "0x{}", bytes_to_hex(self)) write!(f, "0x{}", bytes_to_hex(self))
@ -209,4 +215,11 @@ mod tests {
let b = b.unwrap(); let b = b.unwrap();
assert_eq!(b.as_ref(), hex::decode("1213").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)");
}
} }