@@ -10,7 +10,7 @@ use crate::errors::{StdError, StdResult};
10
10
/// with serde. It also adds some helper methods to help encode inline.
11
11
///
12
12
/// 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 ) ]
14
14
pub struct Binary ( #[ schemars( with = "String" ) ] pub Vec < u8 > ) ;
15
15
16
16
impl Binary {
@@ -75,6 +75,19 @@ impl fmt::Display for Binary {
75
75
}
76
76
}
77
77
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
+
78
91
impl From < & [ u8 ] > for Binary {
79
92
fn from ( binary : & [ u8 ] ) -> Self {
80
93
Self ( binary. to_vec ( ) )
@@ -438,6 +451,17 @@ mod tests {
438
451
assert ! ( res. is_err( ) ) ;
439
452
}
440
453
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
+
441
465
#[ test]
442
466
fn binary_implements_deref ( ) {
443
467
// Dereference to [u8]
0 commit comments