From 9be85406e8ef1f01c2a2a3f1de1a0ac15b02f784 Mon Sep 17 00:00:00 2001 From: Boyu Yang Date: Mon, 11 Jun 2018 22:50:26 +0800 Subject: [PATCH] Prefix '0x' for lower hex with alternate flag. --- fixed-hash/src/hash.rs | 6 ++++-- tests/src/hash_tests.rs | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/fixed-hash/src/hash.rs b/fixed-hash/src/hash.rs index 1d67bed..682a528 100644 --- a/fixed-hash/src/hash.rs +++ b/fixed-hash/src/hash.rs @@ -133,8 +133,7 @@ macro_rules! construct_hash { impl ::core::fmt::Debug for $from { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { - write!(f, "0x")?; - ::core::fmt::LowerHex::fmt(self, f) + write!(f, "{:#x}", self) } } @@ -154,6 +153,9 @@ macro_rules! construct_hash { impl ::core::fmt::LowerHex for $from { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if f.alternate() { + write!(f, "0x"); + } for i in &self.0[..] { write!(f, "{:02x}", i)?; } diff --git a/tests/src/hash_tests.rs b/tests/src/hash_tests.rs index cb3174e..b21b560 100644 --- a/tests/src/hash_tests.rs +++ b/tests/src/hash_tests.rs @@ -7,6 +7,7 @@ fn should_format_and_debug_correctly() { assert_eq!(format!("{}", hash), format!("0x{}", display)); assert_eq!(format!("{:?}", hash), format!("0x{}", hex)); assert_eq!(format!("{:x}", hash), hex); + assert_eq!(format!("{:#x}", hash), format!("0x{}", hex)); }; test(0x1, "00000000000000000000000000000001", "0000…0001");