From bafe11d7e492b12abaac305bda182cd6f820c558 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 30 Oct 2024 16:21:02 +1100 Subject: [PATCH] Correctly feature gate impl_to_hex_from_lower_hex Currently we feature gate code within the `impl_to_hex_from_lower_hex` macro on "alloc" but `bitcoin` does not have the "alloc" feature so this code is never built in. This can be seen by the lack of a `to_hex` function on `LeafVersion`. Remove the feature gate from the macro and put it on the individual call sites as needed. --- internals/src/macros.rs | 1 - primitives/src/pow.rs | 5 ++--- primitives/src/script/mod.rs | 7 ++++--- primitives/src/sequence.rs | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/internals/src/macros.rs b/internals/src/macros.rs index d0ae5bbb1..d1fcd1261 100644 --- a/internals/src/macros.rs +++ b/internals/src/macros.rs @@ -219,7 +219,6 @@ macro_rules! impl_to_hex_from_lower_hex { ($t:ident, $hex_len_fn:expr) => { impl $t { /// Gets the hex representation of this type - #[cfg(feature = "alloc")] pub fn to_hex(&self) -> alloc::string::String { use core::fmt::Write; diff --git a/primitives/src/pow.rs b/primitives/src/pow.rs index 2990c7c45..6a30d7a0b 100644 --- a/primitives/src/pow.rs +++ b/primitives/src/pow.rs @@ -4,8 +4,6 @@ use core::fmt; -use internals::impl_to_hex_from_lower_hex; - /// Encoding of 256-bit target as 32-bit float. /// /// This is used to encode a target into the block header. Satoshi made this part of consensus code @@ -36,7 +34,8 @@ impl fmt::LowerHex for CompactTarget { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) } } -impl_to_hex_from_lower_hex!(CompactTarget, |compact_target: &CompactTarget| { +#[cfg(feature = "alloc")] +internals::impl_to_hex_from_lower_hex!(CompactTarget, |compact_target: &CompactTarget| { 8 - compact_target.0.leading_zeros() as usize / 4 }); diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs index 9635d4e3f..3791165de 100644 --- a/primitives/src/script/mod.rs +++ b/primitives/src/script/mod.rs @@ -12,7 +12,6 @@ use core::fmt; use core::ops::{Deref, DerefMut}; use hex::DisplayHex; -use internals::impl_to_hex_from_lower_hex; use internals::script::{self, PushDataLenLen}; use crate::opcodes::all::*; @@ -231,13 +230,15 @@ impl fmt::LowerHex for Script { fmt::LowerHex::fmt(&self.as_bytes().as_hex(), f) } } -impl_to_hex_from_lower_hex!(Script, |script: &Script| script.len() * 2); +#[cfg(feature = "alloc")] +internals::impl_to_hex_from_lower_hex!(Script, |script: &Script| script.len() * 2); impl fmt::LowerHex for ScriptBuf { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self.as_script(), f) } } -impl_to_hex_from_lower_hex!(ScriptBuf, |script_buf: &ScriptBuf| script_buf.len() * 2); +#[cfg(feature = "alloc")] +internals::impl_to_hex_from_lower_hex!(ScriptBuf, |script_buf: &ScriptBuf| script_buf.len() * 2); impl fmt::UpperHex for Script { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/primitives/src/sequence.rs b/primitives/src/sequence.rs index 41e031937..7bac4f1bf 100644 --- a/primitives/src/sequence.rs +++ b/primitives/src/sequence.rs @@ -19,7 +19,6 @@ use core::fmt; #[cfg(feature = "arbitrary")] use arbitrary::{Arbitrary, Unstructured}; -use internals::impl_to_hex_from_lower_hex; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; #[cfg(feature = "alloc")] @@ -231,7 +230,8 @@ impl fmt::Display for Sequence { impl fmt::LowerHex for Sequence { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) } } -impl_to_hex_from_lower_hex!(Sequence, |sequence: &Sequence| { +#[cfg(feature = "alloc")] +internals::impl_to_hex_from_lower_hex!(Sequence, |sequence: &Sequence| { 8 - sequence.0.leading_zeros() as usize / 4 });