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.
This commit is contained in:
Tobin C. Harding 2024-10-30 16:21:02 +11:00
parent 57769f5f28
commit bafe11d7e4
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
4 changed files with 8 additions and 9 deletions

View File

@ -219,7 +219,6 @@ macro_rules! impl_to_hex_from_lower_hex {
($t:ident, $hex_len_fn:expr) => { ($t:ident, $hex_len_fn:expr) => {
impl $t { impl $t {
/// Gets the hex representation of this type /// Gets the hex representation of this type
#[cfg(feature = "alloc")]
pub fn to_hex(&self) -> alloc::string::String { pub fn to_hex(&self) -> alloc::string::String {
use core::fmt::Write; use core::fmt::Write;

View File

@ -4,8 +4,6 @@
use core::fmt; use core::fmt;
use internals::impl_to_hex_from_lower_hex;
/// Encoding of 256-bit target as 32-bit float. /// 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 /// 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] #[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) } 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 8 - compact_target.0.leading_zeros() as usize / 4
}); });

View File

@ -12,7 +12,6 @@ use core::fmt;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use hex::DisplayHex; use hex::DisplayHex;
use internals::impl_to_hex_from_lower_hex;
use internals::script::{self, PushDataLenLen}; use internals::script::{self, PushDataLenLen};
use crate::opcodes::all::*; use crate::opcodes::all::*;
@ -231,13 +230,15 @@ impl fmt::LowerHex for Script {
fmt::LowerHex::fmt(&self.as_bytes().as_hex(), f) 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 { impl fmt::LowerHex for ScriptBuf {
#[inline] #[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(self.as_script(), f) } 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 { impl fmt::UpperHex for Script {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -19,7 +19,6 @@ use core::fmt;
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured}; use arbitrary::{Arbitrary, Unstructured};
use internals::impl_to_hex_from_lower_hex;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
@ -231,7 +230,8 @@ impl fmt::Display for Sequence {
impl fmt::LowerHex for Sequence { impl fmt::LowerHex for Sequence {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self.0, f) } 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 8 - sequence.0.leading_zeros() as usize / 4
}); });