Skip to content

Commit c441fb6

Browse files
fix(rust/catalyst-types): Implement Display for hash wrappers (#205)
* Implement Display for hash wrappers
1 parent 885d7fc commit c441fb6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

rust/catalyst-types/src/hash_wrapper.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ macro_rules! define_hashes {
3333
}
3434
}
3535

36+
impl std::fmt::Display for $name {
37+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38+
f.write_str(&format!("0x{}", self.0))
39+
}
40+
}
41+
3642
impl From<$name> for Vec<u8> {
3743
fn from(value: $name) -> Self {
3844
value.0.into()
@@ -124,4 +130,14 @@ mod tests {
124130
let from_vec = H1::try_from(v).unwrap();
125131
assert_eq!(hash, from_vec);
126132
}
133+
134+
// The display implementation is used to get user-friendly representation and must be
135+
// equal to `hex::encode(<underlying bytes>)`.
136+
#[test]
137+
fn display() {
138+
let hash = H1::new(&[1, 2, 3, 4, 5]);
139+
let display = format!("{hash}");
140+
let expected = "0x2a6ad53c3c6986406e1d6c7cfd06b69a";
141+
assert_eq!(expected, display);
142+
}
127143
}

0 commit comments

Comments
 (0)