Merge rust-bitcoin/rust-bitcoin#2565: Removes txid prefix in transaction IDs

56132f59d5     Remove the `:#` formatting for `hex_fmt_impl` macro (448 OG)

Pull request description:

  This commit attempts to solve #2505  by ensuring that formatting is not forced using the `:#` in the hex macro code generating in macro rule `hex_fmt_impl` in the hashes/utils.rs file.

  The write! macro forces all formatting to add the prefix `0x` by adding an alternate by (#) default

  ```rust
  impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> {
              #[inline]
              fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
                  write!(f, "{:#}", self) // <-- This is where the formatting is being forced.
              }
          }
  ```

  By removing this formatting, the `:#` must be specified by the user in order for a prefix to be added.

  ```rust
  let outpoint = bitcoin::OutPoint::default();
      println!("{:?}", &outpoint);
      println!("{:#?}", &outpoint);
      println!("{:#}", &outpoint);
      println!("{:x}", &outpoint.txid);
      // `{:#}` must be specified to pretty print with a prefix
      println!("{:#}", &outpoint.txid);
      dbg!(&outpoint);
      dbg!(&outpoint.txid);
  ```

  The PR also adds testcase for this when running `cargo test` .

ACKs for top commit:
  tcharding:
    ACK 56132f59d5
  apoelstra:
    ACK 56132f59d5

Tree-SHA512: 9e4fc9f30ab0b3cf2651d3c09f7f01d8245ac8ea7ae3a82bb4efd19f25c77662bf279020a31fa61b37587cc0c74284696c56045c59f1ba63b2dd42a210d98ebc
This commit is contained in:
Andrew Poelstra 2024-03-13 17:13:15 +00:00
commit 1ceac90bf6
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 24 additions and 1 deletions

View File

@ -2448,6 +2448,29 @@ mod tests {
let seq = Sequence::from_seconds_floor(1000); let seq = Sequence::from_seconds_floor(1000);
println!("{:?}", seq) println!("{:?}", seq)
} }
#[test]
fn outpoint_format() {
let outpoint = OutPoint::default();
let debug = "OutPoint { txid: 0000000000000000000000000000000000000000000000000000000000000000, vout: 4294967295 }";
assert_eq!(debug, format!("{:?}", &outpoint));
let display = "0000000000000000000000000000000000000000000000000000000000000000:4294967295";
assert_eq!(display, format!("{}", &outpoint));
let pretty_debug = "OutPoint {\n txid: 0000000000000000000000000000000000000000000000000000000000000000,\n vout: 4294967295,\n}";
assert_eq!(pretty_debug, format!("{:#?}", &outpoint));
let debug_txid = "0000000000000000000000000000000000000000000000000000000000000000";
assert_eq!(debug_txid, format!("{:?}", &outpoint.txid));
let display_txid = "0000000000000000000000000000000000000000000000000000000000000000";
assert_eq!(display_txid, format!("{}", &outpoint.txid));
let pretty_txid = "0x0000000000000000000000000000000000000000000000000000000000000000";
assert_eq!(pretty_txid, format!("{:#}", &outpoint.txid));
}
} }
#[cfg(bench)] #[cfg(bench)]

View File

@ -39,7 +39,7 @@ macro_rules! hex_fmt_impl(
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> { impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> {
#[inline] #[inline]
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result { fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
write!(f, "{:#}", self) write!(f, "{}", self)
} }
} }
); );