Skip to content

Commit d450501

Browse files
authored
Merge pull request CosmWasm#1201 from CosmWasm/debug-binary-as-hex
Change Debug implementation of Binary to hex
2 parents 9e2b104 + 9554e78 commit d450501

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ and this project adheres to
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- cosmwasm-std: The `Debug` implementation of `Binary` now produces a hex string
12+
instead of a list of bytes ([#1199]).
13+
14+
[#1199]: https://github.com/CosmWasm/cosmwasm/issues/1199
15+
916
## [1.0.0-beta4] - 2021-12-23
1017

1118
### Changed

packages/std/src/binary.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::errors::{StdError, StdResult};
1010
/// with serde. It also adds some helper methods to help encode inline.
1111
///
1212
/// This is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>
13-
#[derive(Clone, Default, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, JsonSchema)]
13+
#[derive(Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord, JsonSchema)]
1414
pub struct Binary(#[schemars(with = "String")] pub Vec<u8>);
1515

1616
impl Binary {
@@ -75,6 +75,19 @@ impl fmt::Display for Binary {
7575
}
7676
}
7777

78+
impl fmt::Debug for Binary {
79+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80+
// Use an output inspired by tuples (https://doc.rust-lang.org/std/fmt/struct.Formatter.html#method.debug_tuple)
81+
// but with a custom implementation to avoid the need for an intemediate hex string.
82+
write!(f, "Binary(")?;
83+
for byte in self.0.iter() {
84+
write!(f, "{:02x}", byte)?;
85+
}
86+
write!(f, ")")?;
87+
Ok(())
88+
}
89+
}
90+
7891
impl From<&[u8]> for Binary {
7992
fn from(binary: &[u8]) -> Self {
8093
Self(binary.to_vec())
@@ -438,6 +451,17 @@ mod tests {
438451
assert!(res.is_err());
439452
}
440453

454+
#[test]
455+
fn binary_implements_debug() {
456+
// Some data
457+
let binary = Binary(vec![0x07, 0x35, 0xAA, 0xcb, 0x00, 0xff]);
458+
assert_eq!(format!("{:?}", binary), "Binary(0735aacb00ff)",);
459+
460+
// Empty
461+
let binary = Binary(vec![]);
462+
assert_eq!(format!("{:?}", binary), "Binary()",);
463+
}
464+
441465
#[test]
442466
fn binary_implements_deref() {
443467
// Dereference to [u8]

packages/vm/src/checksum.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ impl Checksum {
1818
Checksum(Sha256::digest(wasm).into())
1919
}
2020

21-
/// Creates a lowercase hex encoded copy of this checksum
21+
/// Creates a lowercase hex encoded copy of this checksum.
22+
///
23+
/// This takes an owned `self` instead of a reference because `Checksum` is cheap to `Copy`.
2224
pub fn to_hex(self) -> String {
2325
self.to_string()
2426
}

0 commit comments

Comments
 (0)