Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 75c068f

Browse files
committed
Merge #174: Implement Debug and Display using LowerHex
d3da48e Add test to verify hash prints using alternate (Tobin C. Harding) 04be9e2 Implement Display and Debug using LowerHex (Tobin C. Harding) Pull request description: Patch 2 adds a unit test that can be moved to before patch 1 to verify this PR does what it claims. From the commit log of patch 1: `Display` and `LowerHex` offer an 'alternate' option, hex representations by convention should include `0x` if the alternate format is printed. Currently we implement `Display`, `Debug`, and `LowerHex` all using the same macro. This has two problems, the biggest of which is that we are not correctly handling the alternate format. The second issue is code duplication. Modify the `impl_hex_fmt` macro to implement `LowerHex` (incl. alternate form), then implement `Display` and `Debug` in the macro making calls to `LowerHex`. ACKs for top commit: apoelstra: ACK d3da48e Tree-SHA512: d3ea7817f46873f081c6975b6b5724e4baacd1426fd3eb2111964ddb2fb58d1d0c4a2e5ec5cef002cdd6f79593e4740adccbc4cb9a8555843dcfaef5169bb23c
2 parents e740edf + d3da48e commit 75c068f

File tree

9 files changed

+59
-34
lines changed

9 files changed

+59
-34
lines changed

src/hash160.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ pub struct Hash(
3535
[u8; 20]
3636
);
3737

38-
hex_fmt_impl!(Debug, Hash);
39-
hex_fmt_impl!(Display, Hash);
40-
hex_fmt_impl!(LowerHex, Hash);
38+
hex_fmt_impl!(Hash);
4139
serde_impl!(Hash, 20);
4240
borrow_slice_impl!(Hash);
4341

src/ripemd160.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ pub struct Hash(
8383
[u8; 20]
8484
);
8585

86-
hex_fmt_impl!(Debug, Hash);
87-
hex_fmt_impl!(Display, Hash);
88-
hex_fmt_impl!(LowerHex, Hash);
86+
hex_fmt_impl!(Hash);
8987
serde_impl!(Hash, 20);
9088
borrow_slice_impl!(Hash);
9189

src/sha1.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ pub struct Hash(
7878
[u8; 20]
7979
);
8080

81-
hex_fmt_impl!(Debug, Hash);
82-
hex_fmt_impl!(Display, Hash);
83-
hex_fmt_impl!(LowerHex, Hash);
81+
hex_fmt_impl!(Hash);
8482
serde_impl!(Hash, 20);
8583
borrow_slice_impl!(Hash);
8684

src/sha256.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ impl str::FromStr for Hash {
8585
}
8686
}
8787

88-
hex_fmt_impl!(Debug, Hash);
89-
hex_fmt_impl!(Display, Hash);
90-
hex_fmt_impl!(LowerHex, Hash);
88+
hex_fmt_impl!(Hash);
9189
serde_impl!(Hash, 32);
9290
borrow_slice_impl!(Hash);
9391

@@ -168,9 +166,7 @@ impl crate::Hash for Hash {
168166
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
169167
pub struct Midstate(pub [u8; 32]);
170168

171-
hex_fmt_impl!(Debug, Midstate);
172-
hex_fmt_impl!(Display, Midstate);
173-
hex_fmt_impl!(LowerHex, Midstate);
169+
hex_fmt_impl!(Midstate);
174170
serde_impl!(Midstate, 32);
175171
borrow_slice_impl!(Midstate);
176172

src/sha256d.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ pub struct Hash(
3030
[u8; 32]
3131
);
3232

33-
hex_fmt_impl!(Debug, Hash);
34-
hex_fmt_impl!(Display, Hash);
35-
hex_fmt_impl!(LowerHex, Hash);
33+
hex_fmt_impl!(Hash);
3634
serde_impl!(Hash, 32);
3735
borrow_slice_impl!(Hash);
3836

src/sha256t.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ impl<T: Tag> str::FromStr for Hash<T> {
8080
}
8181
}
8282

83-
hex_fmt_impl!(Debug, Hash, T:Tag);
84-
hex_fmt_impl!(Display, Hash, T:Tag);
85-
hex_fmt_impl!(LowerHex, Hash, T:Tag);
83+
hex_fmt_impl!(Hash, T:Tag);
8684
borrow_slice_impl!(Hash, T:Tag);
8785

8886
impl<I: SliceIndex<[u8]>, T: Tag> Index<I> for Hash<T> {

src/sha512.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ impl str::FromStr for Hash {
134134
}
135135
}
136136

137-
hex_fmt_impl!(Debug, Hash);
138-
hex_fmt_impl!(Display, Hash);
139-
hex_fmt_impl!(LowerHex, Hash);
137+
hex_fmt_impl!(Hash);
140138
serde_impl!(Hash, 64);
141139
borrow_slice_impl!(Hash);
142140

src/siphash24.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,7 @@ pub struct Hash(
203203
[u8; 8]
204204
);
205205

206-
hex_fmt_impl!(Debug, Hash);
207-
hex_fmt_impl!(Display, Hash);
208-
hex_fmt_impl!(LowerHex, Hash);
206+
hex_fmt_impl!(Hash);
209207
serde_impl!(Hash, 8);
210208
borrow_slice_impl!(Hash);
211209

src/util.rs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,37 @@
1515
#[macro_export]
1616
/// Adds hexadecimal formatting implementation of a trait `$imp` to a given type `$ty`.
1717
macro_rules! hex_fmt_impl(
18-
($imp:ident, $ty:ident) => (
19-
$crate::hex_fmt_impl!($imp, $ty, );
18+
($ty:ident) => (
19+
$crate::hex_fmt_impl!($ty, );
2020
);
21-
($imp:ident, $ty:ident, $($gen:ident: $gent:ident),*) => (
22-
impl<$($gen: $gent),*> $crate::_export::_core::fmt::$imp for $ty<$($gen),*> {
21+
($ty:ident, $($gen:ident: $gent:ident),*) => (
22+
impl<$($gen: $gent),*> $crate::_export::_core::fmt::LowerHex for $ty<$($gen),*> {
2323
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
2424
#[allow(unused_imports)]
2525
use $crate::{Hash as _, HashEngine as _, hex};
2626

27+
if f.alternate() {
28+
write!(f, "0x")?;
29+
}
2730
if $ty::<$($gen),*>::DISPLAY_BACKWARD {
2831
hex::format_hex_reverse(&self.0, f)
2932
} else {
3033
hex::format_hex(&self.0, f)
3134
}
3235
}
3336
}
37+
38+
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Display for $ty<$($gen),*> {
39+
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
40+
$crate::_export::_core::fmt::LowerHex::fmt(self, f)
41+
}
42+
}
43+
44+
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> {
45+
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
46+
write!(f, "{:#}", self)
47+
}
48+
}
3449
);
3550
);
3651

@@ -169,9 +184,7 @@ macro_rules! hash_newtype {
169184
#[repr(transparent)]
170185
pub struct $newtype($hash);
171186

172-
$crate::hex_fmt_impl!(Debug, $newtype);
173-
$crate::hex_fmt_impl!(Display, $newtype);
174-
$crate::hex_fmt_impl!(LowerHex, $newtype);
187+
$crate::hex_fmt_impl!($newtype);
175188
$crate::serde_impl!($newtype, $len);
176189
$crate::borrow_slice_impl!($newtype);
177190

@@ -310,4 +323,34 @@ mod test {
310323
assert_eq!(u32_to_array_le(0xdeadbeef), [0xef, 0xbe, 0xad, 0xde]);
311324
assert_eq!(u64_to_array_le(0x1badcafedeadbeef), [0xef, 0xbe, 0xad, 0xde, 0xfe, 0xca, 0xad, 0x1b]);
312325
}
326+
327+
hash_newtype!(TestHash, crate::sha256d::Hash, 32, doc="Test hash.");
328+
329+
#[test]
330+
fn display() {
331+
let want = "0000000000000000000000000000000000000000000000000000000000000000";
332+
let got = format!("{}", TestHash::all_zeros());
333+
assert_eq!(got, want)
334+
}
335+
336+
#[test]
337+
fn display_alternate() {
338+
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
339+
let got = format!("{:#}", TestHash::all_zeros());
340+
assert_eq!(got, want)
341+
}
342+
343+
#[test]
344+
fn lower_hex() {
345+
let want = "0000000000000000000000000000000000000000000000000000000000000000";
346+
let got = format!("{:x}", TestHash::all_zeros());
347+
assert_eq!(got, want)
348+
}
349+
350+
#[test]
351+
fn lower_hex_alternate() {
352+
let want = "0x0000000000000000000000000000000000000000000000000000000000000000";
353+
let got = format!("{:#x}", TestHash::all_zeros());
354+
assert_eq!(got, want)
355+
}
313356
}

0 commit comments

Comments
 (0)