Prefix '0x' for lower hex with alternate flag.

This commit is contained in:
Boyu Yang 2018-06-11 22:50:26 +08:00
parent 94b28c822e
commit 9be85406e8
No known key found for this signature in database
GPG Key ID: 8541EBA40EF41840
2 changed files with 5 additions and 2 deletions

View File

@ -133,8 +133,7 @@ macro_rules! construct_hash {
impl ::core::fmt::Debug for $from { impl ::core::fmt::Debug for $from {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "0x")?; write!(f, "{:#x}", self)
::core::fmt::LowerHex::fmt(self, f)
} }
} }
@ -154,6 +153,9 @@ macro_rules! construct_hash {
impl ::core::fmt::LowerHex for $from { impl ::core::fmt::LowerHex for $from {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
if f.alternate() {
write!(f, "0x");
}
for i in &self.0[..] { for i in &self.0[..] {
write!(f, "{:02x}", i)?; write!(f, "{:02x}", i)?;
} }

View File

@ -7,6 +7,7 @@ fn should_format_and_debug_correctly() {
assert_eq!(format!("{}", hash), format!("0x{}", display)); assert_eq!(format!("{}", hash), format!("0x{}", display));
assert_eq!(format!("{:?}", hash), format!("0x{}", hex)); assert_eq!(format!("{:?}", hash), format!("0x{}", hex));
assert_eq!(format!("{:x}", hash), hex); assert_eq!(format!("{:x}", hash), hex);
assert_eq!(format!("{:#x}", hash), format!("0x{}", hex));
}; };
test(0x1, "00000000000000000000000000000001", "0000…0001"); test(0x1, "00000000000000000000000000000001", "0000…0001");