|
15 | 15 | #[macro_export] |
16 | 16 | /// Adds hexadecimal formatting implementation of a trait `$imp` to a given type `$ty`. |
17 | 17 | 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, ); |
20 | 20 | ); |
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),*> { |
23 | 23 | fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result { |
24 | 24 | #[allow(unused_imports)] |
25 | 25 | use $crate::{Hash as _, HashEngine as _, hex}; |
26 | 26 |
|
| 27 | + if f.alternate() { |
| 28 | + write!(f, "0x")?; |
| 29 | + } |
27 | 30 | if $ty::<$($gen),*>::DISPLAY_BACKWARD { |
28 | 31 | hex::format_hex_reverse(&self.0, f) |
29 | 32 | } else { |
30 | 33 | hex::format_hex(&self.0, f) |
31 | 34 | } |
32 | 35 | } |
33 | 36 | } |
| 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 | + } |
34 | 49 | ); |
35 | 50 | ); |
36 | 51 |
|
@@ -169,9 +184,7 @@ macro_rules! hash_newtype { |
169 | 184 | #[repr(transparent)] |
170 | 185 | pub struct $newtype($hash); |
171 | 186 |
|
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); |
175 | 188 | $crate::serde_impl!($newtype, $len); |
176 | 189 | $crate::borrow_slice_impl!($newtype); |
177 | 190 |
|
@@ -310,4 +323,34 @@ mod test { |
310 | 323 | assert_eq!(u32_to_array_le(0xdeadbeef), [0xef, 0xbe, 0xad, 0xde]); |
311 | 324 | assert_eq!(u64_to_array_le(0x1badcafedeadbeef), [0xef, 0xbe, 0xad, 0xde, 0xfe, 0xca, 0xad, 0x1b]); |
312 | 325 | } |
| 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 | + } |
313 | 356 | } |
0 commit comments