Skip to content

Commit 03c0329

Browse files
committed
Merge rust-bitcoin#4662: primitives: Inline impl_to_hex_from_lower_hex macro and deprecate
d42d478 primitives: Inline impl_to_hex_from_lower_hex and deprecate (Tobin C. Harding) Pull request description: Macros are basically just a pain in the arse. This one is used to reduced code duplication/verbosity even thought the function can be written as a one-liner. Also calling macros across crate boundries is error prone and a constant source of maintenance grief. Note also: - The generated docs are quite general - `#[inline]` is missing - The docs on the macro are wrong in regards to the no-op Remove all calls to the macro from `primitives` and inline using terse syntax by way of the `format!` macro. Add `deprecated` attribute and direct users to just use `format!("{var:x}")` instead. ACKs for top commit: apoelstra: ACK d42d478; successfully ran local tests Tree-SHA512: 2029bb18d8d7b83f49da4e947b5feea6e92dcfa73e26439e1b8be278becce553006769280bfb51290fb50af3e28996dc06290aa670283666cae6fc6fec71b10e
2 parents 5e7cd34 + d42d478 commit 03c0329

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

primitives/src/pow.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,18 @@ impl CompactTarget {
3030
/// Returns the consensus encoded `u32` representation of this [`CompactTarget`].
3131
#[inline]
3232
pub fn to_consensus(self) -> u32 { self.0 }
33+
34+
/// Gets the hex representation of this [`CompactTarget`].
35+
#[cfg(feature = "alloc")]
36+
#[inline]
37+
#[deprecated(since = "TBD", note = "use `format!(\"{var:x}\")` instead")]
38+
pub fn to_hex(self) -> alloc::string::String { alloc::format!("{:x}", self) }
3339
}
3440

3541
impl fmt::LowerHex for CompactTarget {
3642
#[inline]
3743
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
3844
}
39-
#[cfg(feature = "alloc")]
40-
internals::impl_to_hex_from_lower_hex!(CompactTarget, |compact_target: &CompactTarget| {
41-
8 - compact_target.0.leading_zeros() as usize / 4
42-
});
4345

4446
impl fmt::UpperHex for CompactTarget {
4547
#[inline]

primitives/src/script/borrowed.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ impl Script {
137137
let inner = unsafe { Box::from_raw(rw) };
138138
ScriptBuf::from_bytes(Vec::from(inner))
139139
}
140+
141+
/// Gets the hex representation of this script.
142+
///
143+
/// # Returns
144+
///
145+
/// Just the script bytes in hexadecimal **not** consensus encoding of the script i.e., the
146+
/// string will not include a length prefix.
147+
#[cfg(feature = "alloc")]
148+
#[cfg(feature = "hex")]
149+
#[inline]
150+
#[deprecated(since = "TBD", note = "use `format!(\"{var:x}\")` instead")]
151+
pub fn to_hex(&self) -> alloc::string::String { alloc::format!("{:x}", self) }
140152
}
141153

142154
#[cfg(feature = "arbitrary")]

primitives/src/script/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,18 +436,12 @@ impl fmt::LowerHex for Script {
436436
fmt::LowerHex::fmt(&self.as_bytes().as_hex(), f)
437437
}
438438
}
439-
#[cfg(feature = "alloc")]
440-
#[cfg(feature = "hex")]
441-
internals::impl_to_hex_from_lower_hex!(Script, |script: &Self| script.len() * 2);
442439

443440
#[cfg(feature = "hex")]
444441
impl fmt::LowerHex for ScriptBuf {
445442
#[inline]
446443
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self.as_script(), f) }
447444
}
448-
#[cfg(feature = "alloc")]
449-
#[cfg(feature = "hex")]
450-
internals::impl_to_hex_from_lower_hex!(ScriptBuf, |script_buf: &Self| script_buf.len() * 2);
451445

452446
#[cfg(feature = "hex")]
453447
impl fmt::UpperHex for Script {
@@ -942,7 +936,7 @@ mod tests {
942936
#[cfg(feature = "hex")]
943937
fn script_to_hex() {
944938
let script = Script::from_bytes(&[0xa1, 0xb2, 0xc3]);
945-
let hex = script.to_hex();
939+
let hex = alloc::format!("{script:x}");
946940
assert_eq!(hex, "a1b2c3");
947941
}
948942

@@ -951,7 +945,7 @@ mod tests {
951945
#[cfg(feature = "hex")]
952946
fn scriptbuf_to_hex() {
953947
let script = ScriptBuf::from_bytes(vec![0xa1, 0xb2, 0xc3]);
954-
let hex = script.to_hex();
948+
let hex = alloc::format!("{script:x}");
955949
assert_eq!(hex, "a1b2c3");
956950
}
957951
}

primitives/src/script/owned.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ impl ScriptBuf {
111111
/// It is guaranteed that `script.capacity() >= script.len()` always holds.
112112
#[inline]
113113
pub fn capacity(&self) -> usize { self.0.capacity() }
114+
115+
/// Gets the hex representation of this script.
116+
///
117+
/// # Returns
118+
///
119+
/// Just the script bytes in hexadecimal **not** consensus encoding of the script i.e., the
120+
/// string will not include a length prefix.
121+
#[cfg(feature = "alloc")]
122+
#[cfg(feature = "hex")]
123+
#[inline]
124+
#[deprecated(since = "TBD", note = "use `format!(\"{var:x}\")` instead")]
125+
pub fn to_hex(&self) -> alloc::string::String { alloc::format!("{:x}", self) }
114126
}
115127

116128
impl Deref for ScriptBuf {

primitives/src/sequence.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use units::parse::{self, PrefixedHexError, UnprefixedHexError};
2525

2626
use crate::locktime::relative;
2727
#[cfg(all(doc, feature = "alloc"))]
28-
use crate::transaction::Transaction;
28+
use crate::Transaction;
2929

3030
/// Bitcoin transaction input sequence number.
3131
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@@ -178,6 +178,12 @@ impl Sequence {
178178
#[inline]
179179
pub fn to_consensus_u32(self) -> u32 { self.0 }
180180

181+
/// Gets the hex representation of this [`Sequence`].
182+
#[cfg(feature = "alloc")]
183+
#[inline]
184+
#[deprecated(since = "TBD", note = "use `format!(\"{var:x}\")` instead")]
185+
pub fn to_hex(self) -> alloc::string::String { alloc::format!("{:x}", self) }
186+
181187
/// Constructs a new [`relative::LockTime`] from this [`Sequence`] number.
182188
#[inline]
183189
pub fn to_relative_lock_time(self) -> Option<relative::LockTime> {
@@ -223,10 +229,6 @@ impl fmt::LowerHex for Sequence {
223229
#[inline]
224230
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) }
225231
}
226-
#[cfg(feature = "alloc")]
227-
internals::impl_to_hex_from_lower_hex!(Sequence, |sequence: &Sequence| {
228-
8 - sequence.0.leading_zeros() as usize / 4
229-
});
230232

231233
impl fmt::UpperHex for Sequence {
232234
#[inline]

0 commit comments

Comments
 (0)