From b20672e649855f22605dba4ad78d53bb4a6f2849 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Fri, 26 Nov 2021 11:43:46 +0100 Subject: [PATCH 1/4] feat: implement hex display for Bytes --- ethers-core/src/types/bytes.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ethers-core/src/types/bytes.rs b/ethers-core/src/types/bytes.rs index f21797114..58126bfe9 100644 --- a/ethers-core/src/types/bytes.rs +++ b/ethers-core/src/types/bytes.rs @@ -3,6 +3,8 @@ use serde::{ Deserialize, Deserializer, Serialize, Serializer, }; +use std::fmt::{Formatter, LowerHex, Result as FmtResult}; + /// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings #[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd)] pub struct Bytes( @@ -10,6 +12,13 @@ pub struct Bytes( pub bytes::Bytes, ); +impl LowerHex for Bytes { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + let hex = hex::encode(self.0.as_ref()); + write!(f, "{}", hex) + } +} + impl Bytes { pub fn to_vec(&self) -> Vec { self.as_ref().to_vec() @@ -67,3 +76,15 @@ where Err(Error::invalid_value(Unexpected::Str(&value), &"0x prefix")) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn hex_formatting() { + let b = Bytes::from(vec![1, 35, 69, 103, 137, 171, 205, 239]); + let expected = String::from("0123456789abcdef"); + assert_eq!(format!("{:x}", b), expected); + } +} From c87c6e8f7a7cf6bb2f462c5bac3ed0dfdf07e0ca Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Fri, 26 Nov 2021 11:52:32 +0100 Subject: [PATCH 2/4] chore: add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 471af9cb1..6385b314c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Fix `format_units` to return a `String` of representing a decimal point float such that the decimal places don't get truncated. [597](https://github.com/gakonst/ethers-rs/pull/597) +- Implement hex display format for `ethers::core::Bytes` [#624](https://github.com/gakonst/ethers-rs/pull/624). ### Unreleased From a7370d4142bb263493f8eaaaffd1f743dbb0a4e5 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Fri, 26 Nov 2021 12:30:36 +0100 Subject: [PATCH 3/4] feat: impl Display, same as LowerHex --- ethers-core/src/types/bytes.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ethers-core/src/types/bytes.rs b/ethers-core/src/types/bytes.rs index 58126bfe9..311a45ddf 100644 --- a/ethers-core/src/types/bytes.rs +++ b/ethers-core/src/types/bytes.rs @@ -3,7 +3,7 @@ use serde::{ Deserialize, Deserializer, Serialize, Serializer, }; -use std::fmt::{Formatter, LowerHex, Result as FmtResult}; +use std::fmt::{Display, Formatter, LowerHex, Result as FmtResult}; /// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings #[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd)] @@ -12,10 +12,19 @@ pub struct Bytes( pub bytes::Bytes, ); +fn bytes_to_hex(b: &Bytes) -> String { + hex::encode(b.0.as_ref()) +} + +impl Display for Bytes { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + write!(f, "{}", bytes_to_hex(self)) + } +} + impl LowerHex for Bytes { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - let hex = hex::encode(self.0.as_ref()); - write!(f, "{}", hex) + write!(f, "{}", bytes_to_hex(self)) } } @@ -86,5 +95,6 @@ mod tests { let b = Bytes::from(vec![1, 35, 69, 103, 137, 171, 205, 239]); let expected = String::from("0123456789abcdef"); assert_eq!(format!("{:x}", b), expected); + assert_eq!(format!("{}", b), expected); } } From f7dd4254d82baf725cca8c03ea333bc16cdecf99 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Fri, 26 Nov 2021 12:35:24 +0100 Subject: [PATCH 4/4] fix: prepend 0x for hex display --- ethers-core/src/types/bytes.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethers-core/src/types/bytes.rs b/ethers-core/src/types/bytes.rs index 311a45ddf..9ec302630 100644 --- a/ethers-core/src/types/bytes.rs +++ b/ethers-core/src/types/bytes.rs @@ -18,13 +18,13 @@ fn bytes_to_hex(b: &Bytes) -> String { impl Display for Bytes { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - write!(f, "{}", bytes_to_hex(self)) + write!(f, "0x{}", bytes_to_hex(self)) } } impl LowerHex for Bytes { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - write!(f, "{}", bytes_to_hex(self)) + write!(f, "0x{}", bytes_to_hex(self)) } } @@ -93,7 +93,7 @@ mod tests { #[test] fn hex_formatting() { let b = Bytes::from(vec![1, 35, 69, 103, 137, 171, 205, 239]); - let expected = String::from("0123456789abcdef"); + let expected = String::from("0x0123456789abcdef"); assert_eq!(format!("{:x}", b), expected); assert_eq!(format!("{}", b), expected); }