Merge rust-bitcoin/rust-bitcoin#1204: [update] macro to implement Debug from Display and add Display implementation where needed

4c1dc4594d  Implementing a macro to define debug from display and adding  Display trait implementation where needed (hrouis)

Pull request description:

  Related to issue : [1201](https://github.com/rust-bitcoin/rust-bitcoin/issues/1201)

  > Replaced macro **impl_display_from_debug** with **impl_debug_from_display**

  > Added implementation of Display trait where needed.

ACKs for top commit:
  Kixunil:
    ACK 4c1dc4594d
  apoelstra:
    ACK 4c1dc4594d

Tree-SHA512: a240ccd507ac957ed82adf14623d2b77ee7a19458c2617b69f440eba8ebe52d4b45c1328198116ee720aeee418f6390238772eed002f850cc1bfbdc0248b606e
This commit is contained in:
Andrew Poelstra 2022-08-24 17:06:12 +00:00
commit 5a98e4a5a5
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 50 additions and 35 deletions

View File

@ -14,7 +14,7 @@
#[cfg(feature = "serde")] use crate::prelude::*; #[cfg(feature = "serde")] use crate::prelude::*;
use core::{fmt, convert::From}; use core::{fmt, convert::From};
use crate::internal_macros::display_from_debug; use crate::internal_macros::debug_from_display;
// Note: I am deliberately not implementing PartialOrd or Ord on the // Note: I am deliberately not implementing PartialOrd or Ord on the
// opcode enum. If you want to check ranges of opcodes, etc., // opcode enum. If you want to check ranges of opcodes, etc.,
@ -549,7 +549,7 @@ pub mod all {
pub const OP_INVALIDOPCODE: All = All {code: 0xff}; pub const OP_INVALIDOPCODE: All = All {code: 0xff};
} }
impl fmt::Debug for All { impl fmt::Display for All {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("OP_")?; f.write_str("OP_")?;
match *self { match *self {
@ -745,7 +745,7 @@ impl From<u8> for All {
} }
} }
display_from_debug!(All); debug_from_display!(All);
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
@ -786,19 +786,6 @@ pub enum Class {
Ordinary(Ordinary) Ordinary(Ordinary)
} }
display_from_debug!(Class);
#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl serde::Serialize for Class {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
macro_rules! ordinary_opcode { macro_rules! ordinary_opcode {
($($op:ident),*) => ( ($($op:ident),*) => (
#[repr(u8)] #[repr(u8)]
@ -808,6 +795,14 @@ macro_rules! ordinary_opcode {
$( $op = all::$op.code ),* $( $op = all::$op.code ),*
} }
impl fmt::Display for Ordinary {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
$(Ordinary::$op => { f.pad(stringify!($op)) }),*
}
}
}
impl Ordinary { impl Ordinary {
fn with(b: All) -> Self { fn with(b: All) -> Self {
match b { match b {
@ -866,7 +861,6 @@ impl Ordinary {
pub fn to_u8(self) -> u8 { pub fn to_u8(self) -> u8 {
self as u8 self as u8
} }
} }
#[cfg(test)] #[cfg(test)]

View File

@ -18,7 +18,7 @@ use crate::io;
use core::convert::TryFrom; use core::convert::TryFrom;
use core::{fmt, default::Default}; use core::{fmt, default::Default};
use core::ops::Index; use core::ops::Index;
use crate::internal_macros::display_from_debug; use crate::internal_macros::debug_from_display;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
@ -121,9 +121,16 @@ impl core::str::FromStr for Script {
} }
/// An object which can be used to construct a script piece by piece. /// An object which can be used to construct a script piece by piece.
#[derive(PartialEq, Eq, Debug, Clone)] #[derive(PartialEq, Eq, Clone)]
pub struct Builder(Vec<u8>, Option<opcodes::All>); pub struct Builder(Vec<u8>, Option<opcodes::All>);
display_from_debug!(Builder);
impl fmt::Display for Builder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Script::bytes_to_asm_fmt(self.0.as_ref(), f)
}
}
debug_from_display!(Builder);
impl<I> Index<I> for Builder impl<I> Index<I> for Builder
where where

View File

@ -107,16 +107,16 @@ macro_rules! impl_array_newtype {
} }
pub(crate) use impl_array_newtype; pub(crate) use impl_array_newtype;
macro_rules! display_from_debug { macro_rules! debug_from_display {
($thing:ident) => { ($thing:ident) => {
impl core::fmt::Display for $thing { impl core::fmt::Debug for $thing {
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> { fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
core::fmt::Debug::fmt(self, f) core::fmt::Display::fmt(self, f)
} }
} }
}; };
} }
pub(crate) use display_from_debug; pub(crate) use debug_from_display;
#[cfg(test)] #[cfg(test)]
macro_rules! hex_script (($s:expr) => (<$crate::Script as core::str::FromStr>::from_str($s).unwrap())); macro_rules! hex_script (($s:expr) => (<$crate::Script as core::str::FromStr>::from_str($s).unwrap()));

View File

@ -127,7 +127,7 @@ macro_rules! construct_uint {
let your_bits = other.bits(); let your_bits = other.bits();
// Check for division by 0 // Check for division by 0
assert!(your_bits != 0, "attempted to divide {} by zero", self); assert!(your_bits != 0, "attempted to divide {:#x} by zero", self);
// Early return in case we are dividing by a larger number than us // Early return in case we are dividing by a larger number than us
if my_bits < your_bits { if my_bits < your_bits {
@ -393,10 +393,12 @@ macro_rules! construct_uint {
} }
} }
impl core::fmt::Debug for $name { impl core::fmt::LowerHex for $name {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let &$name(ref data) = self; let &$name(ref data) = self;
if f.alternate() {
write!(f, "0x")?; write!(f, "0x")?;
}
for ch in data.iter().rev() { for ch in data.iter().rev() {
write!(f, "{:016x}", ch)?; write!(f, "{:016x}", ch)?;
} }
@ -404,7 +406,11 @@ macro_rules! construct_uint {
} }
} }
$crate::internal_macros::display_from_debug!($name); impl core::fmt::Debug for $name {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f,"{:#x}", self)
}
}
impl $crate::consensus::Encodable for $name { impl $crate::consensus::Encodable for $name {
#[inline] #[inline]
@ -565,17 +571,25 @@ mod tests {
#[test] #[test]
pub fn uint256_display_test() { pub fn uint256_display_test() {
assert_eq!(format!("{}", Uint256::from_u64(0xDEADBEEF).unwrap()), assert_eq!(format!("{:#x}", Uint256::from_u64(0xDEADBEEF).unwrap()),
"0x00000000000000000000000000000000000000000000000000000000deadbeef"); "0x00000000000000000000000000000000000000000000000000000000deadbeef");
assert_eq!(format!("{}", Uint256::from_u64(u64::max_value()).unwrap()), assert_eq!(format!("{:x}", Uint256::from_u64(0xDEADBEEF).unwrap()),
"00000000000000000000000000000000000000000000000000000000deadbeef");
assert_eq!(format!("{:#x}", Uint256::from_u64(u64::max_value()).unwrap()),
"0x000000000000000000000000000000000000000000000000ffffffffffffffff"); "0x000000000000000000000000000000000000000000000000ffffffffffffffff");
let max_val = Uint256([0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, let max_val = Uint256([0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF]); 0xFFFFFFFFFFFFFFFF]);
assert_eq!(format!("{}", max_val), assert_eq!(format!("{:#x}", max_val),
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
} }
#[test]
pub fn uint256_debug_test() {
assert_eq!(format!("{:?}", Uint256::from_u64(0xDEADBEEF).unwrap()),
"0x00000000000000000000000000000000000000000000000000000000deadbeef");
}
#[test] #[test]
pub fn uint256_comp_test() { pub fn uint256_comp_test() {
let small = Uint256([10u64, 0, 0, 0]); let small = Uint256([10u64, 0, 0, 0]);