Use pub(crate) for macros instead of macro_use

For internal macros used only in this crate we do not need to use
`macro_use` and pollute the top level namespace now that we have edition
2018. We can add a `pub(crate) use` statement to each and then path
imports work for the macros like normal types.
This commit is contained in:
Tobin C. Harding 2022-06-07 13:25:27 +10:00
parent 23ee0930c7
commit 21a1cc791c
29 changed files with 49 additions and 8 deletions

View File

@ -25,6 +25,7 @@ use crate::blockdata::transaction::Transaction;
use crate::blockdata::constants::{max_target, WITNESS_SCALE_FACTOR}; use crate::blockdata::constants::{max_target, WITNESS_SCALE_FACTOR};
use crate::blockdata::script; use crate::blockdata::script;
use crate::VarInt; use crate::VarInt;
use crate::internal_macros::impl_consensus_encoding;
/// Bitcoin block header. /// Bitcoin block header.
/// ///

View File

@ -21,6 +21,7 @@ use crate::blockdata::block::{Block, BlockHeader};
use crate::blockdata::witness::Witness; use crate::blockdata::witness::Witness;
use crate::network::constants::Network; use crate::network::constants::Network;
use crate::util::uint::Uint256; use crate::util::uint::Uint256;
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype};
/// The maximum allowable sequence number /// The maximum allowable sequence number
pub const MAX_SEQUENCE: u32 = 0xFFFFFFFF; pub const MAX_SEQUENCE: u32 = 0xFFFFFFFF;

View File

@ -14,6 +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;
// 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.,

View File

@ -18,6 +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;
#[cfg(feature = "serde")] use serde; #[cfg(feature = "serde")] use serde;
@ -1092,6 +1093,7 @@ mod test {
use crate::blockdata::opcodes; use crate::blockdata::opcodes;
use crate::util::key::PublicKey; use crate::util::key::PublicKey;
use crate::util::psbt::serialize::Serialize; use crate::util::psbt::serialize::Serialize;
use crate::internal_macros::hex_script;
#[test] #[test]
fn script() { fn script() {

View File

@ -30,6 +30,7 @@ use crate::consensus::{encode, Decodable, Encodable};
use crate::hash_types::{Sighash, Txid, Wtxid}; use crate::hash_types::{Sighash, Txid, Wtxid};
use crate::VarInt; use crate::VarInt;
use crate::util::sighash::UINT256_ONE; use crate::util::sighash::UINT256_ONE;
use crate::internal_macros::{impl_consensus_encoding, serde_string_impl, serde_struct_human_string_impl, write_err};
#[cfg(doc)] #[cfg(doc)]
use crate::util::sighash::SchnorrSighashType; use crate::util::sighash::SchnorrSighashType;

View File

@ -22,7 +22,7 @@ use core::{fmt, mem, u32, convert::From};
use crate::hashes::{sha256d, Hash, sha256}; use crate::hashes::{sha256d, Hash, sha256};
use crate::hash_types::{BlockHash, FilterHash, TxMerkleNode, FilterHeader}; use crate::hash_types::{BlockHash, FilterHash, TxMerkleNode, FilterHeader};
use crate::internal_macros::write_err;
use crate::io::{self, Cursor, Read}; use crate::io::{self, Cursor, Read};
use crate::util::endian; use crate::util::endian;
@ -231,7 +231,7 @@ macro_rules! decoder_fn {
($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => { ($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => {
#[inline] #[inline]
fn $name(&mut self) -> Result<$val_type, Error> { fn $name(&mut self) -> Result<$val_type, Error> {
const_assert!(::core::mem::size_of::<$val_type>() == $byte_len); $crate::internal_macros::const_assert!(::core::mem::size_of::<$val_type>() == $byte_len);
let mut val = [0; $byte_len]; let mut val = [0; $byte_len];
self.read_exact(&mut val[..]).map_err(Error::Io)?; self.read_exact(&mut val[..]).map_err(Error::Io)?;
Ok(endian::$readfn(&val)) Ok(endian::$readfn(&val))

View File

@ -44,6 +44,7 @@ macro_rules! impl_consensus_encoding {
} }
); );
} }
pub(crate) use impl_consensus_encoding;
/// Implements standard array methods for a given wrapper type /// Implements standard array methods for a given wrapper type
macro_rules! impl_array_newtype { macro_rules! impl_array_newtype {
@ -104,6 +105,7 @@ macro_rules! impl_array_newtype {
} }
}; };
} }
pub(crate) use impl_array_newtype;
macro_rules! display_from_debug { macro_rules! display_from_debug {
($thing:ident) => { ($thing:ident) => {
@ -114,15 +116,22 @@ macro_rules! display_from_debug {
} }
}; };
} }
pub(crate) use display_from_debug;
#[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()));
#[cfg(test)]
pub(crate) use hex_script;
#[cfg(test)] #[cfg(test)]
macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(&<$crate::prelude::Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap())); macro_rules! hex_hash (($h:ident, $s:expr) => ($h::from_slice(&<$crate::prelude::Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap()));
#[cfg(test)]
pub(crate) use hex_hash;
#[cfg(test)] #[cfg(test)]
macro_rules! hex_decode (($h:ident, $s:expr) => (deserialize::<$h>(&<$crate::prelude::Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap())); macro_rules! hex_decode (($h:ident, $s:expr) => (deserialize::<$h>(&<$crate::prelude::Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap()));
#[cfg(test)]
pub(crate) use hex_decode;
macro_rules! serde_string_impl { macro_rules! serde_string_impl {
($name:ident, $expecting:literal) => { ($name:ident, $expecting:literal) => {
@ -168,6 +177,7 @@ macro_rules! serde_string_impl {
} }
}; };
} }
pub(crate) use serde_string_impl;
/// A combination macro where the human-readable serialization is done like /// A combination macro where the human-readable serialization is done like
/// serde_string_impl and the non-human-readable impl is done as a struct. /// serde_string_impl and the non-human-readable impl is done as a struct.
@ -342,6 +352,7 @@ macro_rules! serde_struct_human_string_impl {
} }
) )
} }
pub(crate) use serde_struct_human_string_impl;
/// Implements several traits for byte-based newtypes. /// Implements several traits for byte-based newtypes.
/// Implements: /// Implements:
@ -476,6 +487,7 @@ macro_rules! impl_bytes_newtype {
} }
}; };
} }
pub(crate) use impl_bytes_newtype;
macro_rules! user_enum { macro_rules! user_enum {
( (
@ -563,6 +575,7 @@ macro_rules! user_enum {
} }
); );
} }
pub(crate) use user_enum;
/// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this /// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this
/// because `e.source()` is only available in std builds, without this macro the error source is /// because `e.source()` is only available in std builds, without this macro the error source is
@ -582,6 +595,7 @@ macro_rules! write_err {
} }
} }
} }
pub(crate) use write_err;
/// Asserts a boolean expression at compile time. /// Asserts a boolean expression at compile time.
macro_rules! const_assert { macro_rules! const_assert {
@ -589,3 +603,4 @@ macro_rules! const_assert {
const _: [(); 0 - !$x as usize] = []; const _: [(); 0 - !$x as usize] = [];
}}; }};
} }
pub(crate) use const_assert;

View File

@ -75,7 +75,6 @@ extern crate actual_serde as serde;
#[cfg(test)] #[cfg(test)]
#[macro_use] #[macro_use]
mod test_macros; mod test_macros;
#[macro_use]
mod internal_macros; mod internal_macros;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
mod serde_utils; mod serde_utils;

View File

@ -30,6 +30,7 @@ use core::{fmt, ops, convert::From};
use crate::io; use crate::io;
use crate::consensus::encode::{self, Encodable, Decodable}; use crate::consensus::encode::{self, Encodable, Decodable};
use crate::internal_macros::user_enum;
/// Version of the protocol as appearing in network message headers /// Version of the protocol as appearing in network message headers
/// This constant is used to signal to other peers which features you support. /// This constant is used to signal to other peers which features you support.

View File

@ -16,6 +16,7 @@ use crate::hashes::{Hash as _, sha256d};
use crate::network::constants; use crate::network::constants;
use crate::consensus::encode::{self, Decodable, Encodable}; use crate::consensus::encode::{self, Decodable, Encodable};
use crate::hash_types::{BlockHash, Txid, Wtxid}; use crate::hash_types::{BlockHash, Txid, Wtxid};
use crate::internal_macros::impl_consensus_encoding;
/// An inventory item. /// An inventory item.
#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash, PartialOrd, Ord)] #[derive(PartialEq, Eq, Clone, Debug, Copy, Hash, PartialOrd, Ord)]

View File

@ -7,6 +7,7 @@
use crate::consensus::encode; use crate::consensus::encode;
use crate::consensus::{Decodable, Encodable, ReadExt}; use crate::consensus::{Decodable, Encodable, ReadExt};
use crate::internal_macros::impl_consensus_encoding;
use std::io; use std::io;
/// `filterload` message sets the current bloom filter /// `filterload` message sets the current bloom filter

View File

@ -6,6 +6,7 @@
//! //!
use crate::hash_types::{BlockHash, FilterHash, FilterHeader}; use crate::hash_types::{BlockHash, FilterHash, FilterHeader};
use crate::internal_macros::impl_consensus_encoding;
/// getcfilters message /// getcfilters message
#[derive(PartialEq, Eq, Clone, Debug)] #[derive(PartialEq, Eq, Clone, Debug)]

View File

@ -16,6 +16,7 @@ use crate::network::constants::{self, ServiceFlags};
use crate::consensus::{Encodable, Decodable, ReadExt}; use crate::consensus::{Encodable, Decodable, ReadExt};
use crate::consensus::encode; use crate::consensus::encode;
use crate::hashes::sha256d; use crate::hashes::sha256d;
use crate::internal_macros::impl_consensus_encoding;
/// Some simple messages /// Some simple messages

View File

@ -41,6 +41,7 @@ use crate::util::taproot::TapBranchHash;
use crate::util::key::PublicKey; use crate::util::key::PublicKey;
use crate::blockdata::script::Instruction; use crate::blockdata::script::Instruction;
use crate::util::schnorr::{TapTweak, UntweakedPublicKey, TweakedPublicKey}; use crate::util::schnorr::{TapTweak, UntweakedPublicKey, TweakedPublicKey};
use crate::internal_macros::{serde_string_impl, write_err};
/// Address error. /// Address error.
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]

View File

@ -15,6 +15,7 @@ use crate::hashes::{sha256d, Hash, hex};
use secp256k1; use secp256k1;
use crate::util::{endian, key}; use crate::util::{endian, key};
use crate::internal_macros::write_err;
/// An error that might occur during base58 decoding /// An error that might occur during base58 decoding
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]

View File

@ -186,6 +186,7 @@ mod tests {
use crate::util::address::Address; use crate::util::address::Address;
use crate::util::key::PublicKey; use crate::util::key::PublicKey;
use crate::hashes::hex::FromHex; use crate::hashes::hex::FromHex;
use crate::internal_macros::{hex_hash, hex_script};
use super::*; use super::*;

View File

@ -49,6 +49,7 @@ use crate::blockdata::transaction::OutPoint;
use crate::consensus::{Decodable, Encodable}; use crate::consensus::{Decodable, Encodable};
use crate::consensus::encode::VarInt; use crate::consensus::encode::VarInt;
use crate::util::endian; use crate::util::endian;
use crate::internal_macros::write_err;
/// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845 /// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845
const P: u8 = 19; const P: u8 = 19;

View File

@ -21,6 +21,7 @@ use secp256k1::{self, Secp256k1, XOnlyPublicKey};
use crate::network::constants::Network; use crate::network::constants::Network;
use crate::util::{base58, endian, key}; use crate::util::{base58, endian, key};
use crate::util::key::{PublicKey, PrivateKey, KeyPair}; use crate::util::key::{PublicKey, PrivateKey, KeyPair};
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype, serde_string_impl, write_err};
/// A chain code /// A chain code
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

View File

@ -12,6 +12,7 @@ use crate::hashes::hex::{self, FromHex};
use crate::blockdata::transaction::NonStandardSighashType; use crate::blockdata::transaction::NonStandardSighashType;
use secp256k1; use secp256k1;
use crate::EcdsaSighashType; use crate::EcdsaSighashType;
use crate::internal_macros::write_err;
/// An ECDSA signature with the corresponding hash type. /// An ECDSA signature with the corresponding hash type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

View File

@ -30,7 +30,7 @@ macro_rules! define_be_to_array {
($name: ident, $type: ty, $byte_len: expr) => { ($name: ident, $type: ty, $byte_len: expr) => {
#[inline] #[inline]
pub fn $name(val: $type) -> [u8; $byte_len] { pub fn $name(val: $type) -> [u8; $byte_len] {
const_assert!(::core::mem::size_of::<$type>() == $byte_len); $crate::internal_macros::const_assert!(::core::mem::size_of::<$type>() == $byte_len);
let mut res = [0; $byte_len]; let mut res = [0; $byte_len];
for i in 0..$byte_len { for i in 0..$byte_len {
res[i] = ((val >> ($byte_len - i - 1)*8) & 0xff) as u8; res[i] = ((val >> ($byte_len - i - 1)*8) & 0xff) as u8;
@ -43,7 +43,7 @@ macro_rules! define_le_to_array {
($name: ident, $type: ty, $byte_len: expr) => { ($name: ident, $type: ty, $byte_len: expr) => {
#[inline] #[inline]
pub fn $name(val: $type) -> [u8; $byte_len] { pub fn $name(val: $type) -> [u8; $byte_len] {
const_assert!(::core::mem::size_of::<$type>() == $byte_len); $crate::internal_macros::const_assert!(::core::mem::size_of::<$type>() == $byte_len);
let mut res = [0; $byte_len]; let mut res = [0; $byte_len];
for i in 0..$byte_len { for i in 0..$byte_len {
res[i] = ((val >> i*8) & 0xff) as u8; res[i] = ((val >> i*8) & 0xff) as u8;

View File

@ -18,6 +18,7 @@ use crate::network::constants::Network;
use crate::hashes::{Hash, hash160, hex, hex::FromHex}; use crate::hashes::{Hash, hash160, hex, hex::FromHex};
use crate::hash_types::{PubkeyHash, WPubkeyHash}; use crate::hash_types::{PubkeyHash, WPubkeyHash};
use crate::util::base58; use crate::util::base58;
use crate::internal_macros::write_err;
/// A key-related error. /// A key-related error.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]

View File

@ -33,6 +33,7 @@ mod message_signing {
use crate::util::key::PublicKey; use crate::util::key::PublicKey;
use crate::util::address::{Address, AddressType}; use crate::util::address::{Address, AddressType};
use crate::internal_macros::write_err;
/// An error used for dealing with Bitcoin Signed Messages. /// An error used for dealing with Bitcoin Signed Messages.
#[cfg_attr(docsrs, doc(cfg(feature = "secp-recovery")))] #[cfg_attr(docsrs, doc(cfg(feature = "secp-recovery")))]

View File

@ -30,6 +30,7 @@ use crate::io;
use core::fmt; use core::fmt;
use crate::consensus::encode; use crate::consensus::encode;
use crate::internal_macros::write_err;
/// A trait which allows numbers to act as fixed-size bit arrays /// A trait which allows numbers to act as fixed-size bit arrays
pub trait BitArray { pub trait BitArray {

View File

@ -10,6 +10,7 @@ use crate::util::psbt::raw;
use crate::hashes; use crate::hashes;
use crate::util::bip32::ExtendedPubKey; use crate::util::bip32::ExtendedPubKey;
use crate::internal_macros::write_err;
/// Enum for marking psbt hash error. /// Enum for marking psbt hash error.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]

View File

@ -207,6 +207,7 @@ mod display_from_str {
use core::str::FromStr; use core::str::FromStr;
use crate::consensus::encode::{Error, self}; use crate::consensus::encode::{Error, self};
use base64::display::Base64Display; use base64::display::Base64Display;
use crate::internal_macros::write_err;
/// Error encountered during PSBT decoding from Base64 string. /// Error encountered during PSBT decoding from Base64 string.
#[derive(Debug)] #[derive(Debug)]
@ -347,6 +348,7 @@ mod tests {
use crate::util::bip32::{ChildNumber, ExtendedPrivKey, ExtendedPubKey, KeySource}; use crate::util::bip32::{ChildNumber, ExtendedPrivKey, ExtendedPubKey, KeySource};
use crate::util::psbt::map::{Output, Input}; use crate::util::psbt::map::{Output, Input};
use crate::util::psbt::raw; use crate::util::psbt::raw;
use crate::internal_macros::hex_script;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::blockdata::witness::Witness; use crate::blockdata::witness::Witness;
@ -602,6 +604,7 @@ mod tests {
use crate::util::psbt::{PartiallySignedTransaction, Error}; use crate::util::psbt::{PartiallySignedTransaction, Error};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::blockdata::witness::Witness; use crate::blockdata::witness::Witness;
use crate::internal_macros::hex_script;
#[test] #[test]
#[should_panic(expected = "InvalidMagic")] #[should_panic(expected = "InvalidMagic")]

View File

@ -14,6 +14,7 @@ use crate::prelude::*;
use secp256k1::{self, Secp256k1, Verification, constants}; use secp256k1::{self, Secp256k1, Verification, constants};
use crate::util::taproot::{TapBranchHash, TapTweakHash}; use crate::util::taproot::{TapBranchHash, TapTweakHash};
use crate::SchnorrSighashType; use crate::SchnorrSighashType;
use crate::internal_macros::write_err;
/// Deprecated re-export of [`secp256k1::XOnlyPublicKey`] /// Deprecated re-export of [`secp256k1::XOnlyPublicKey`]
#[deprecated(since = "0.28.0", note = "Please use `util::key::XOnlyPublicKey` instead")] #[deprecated(since = "0.28.0", note = "Please use `util::key::XOnlyPublicKey` instead")]

View File

@ -22,6 +22,7 @@ use crate::io;
use crate::util::taproot::{TapLeafHash, TAPROOT_ANNEX_PREFIX, TapSighashHash}; use crate::util::taproot::{TapLeafHash, TAPROOT_ANNEX_PREFIX, TapSighashHash};
use crate::Sighash; use crate::Sighash;
use crate::{Script, Transaction, TxOut}; use crate::{Script, Transaction, TxOut};
use crate::internal_macros::serde_string_impl;
use super::taproot::LeafVersion; use super::taproot::LeafVersion;
@ -819,6 +820,7 @@ mod tests {
use crate::hashes::hex::ToHex; use crate::hashes::hex::ToHex;
use crate::util::taproot::{TapTweakHash, TapSighashHash, TapBranchHash, TapLeafHash}; use crate::util::taproot::{TapTweakHash, TapSighashHash, TapBranchHash, TapLeafHash};
use secp256k1::{self, SecretKey, XOnlyPublicKey}; use secp256k1::{self, SecretKey, XOnlyPublicKey};
use crate::internal_macros::{hex_hash, hex_script, hex_decode};
extern crate serde_json; extern crate serde_json;
use crate::{Script, Transaction, TxIn, TxOut}; use crate::{Script, Transaction, TxIn, TxOut};

View File

@ -17,7 +17,7 @@ use crate::hashes::{sha256, sha256t_hash_newtype, Hash, HashEngine};
use crate::schnorr::{TweakedPublicKey, UntweakedPublicKey, TapTweak}; use crate::schnorr::{TweakedPublicKey, UntweakedPublicKey, TapTweak};
use crate::util::key::XOnlyPublicKey; use crate::util::key::XOnlyPublicKey;
use crate::Script; use crate::Script;
use crate::internal_macros::write_err;
use crate::consensus::Encodable; use crate::consensus::Encodable;
/// The SHA-256 midstate value for the TapLeaf hash. /// The SHA-256 midstate value for the TapLeaf hash.

View File

@ -12,7 +12,7 @@ macro_rules! construct_uint {
/// Little-endian large integer type /// Little-endian large integer type
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default)] #[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
pub struct $name(pub [u64; $n_words]); pub struct $name(pub [u64; $n_words]);
impl_array_newtype!($name, u64, $n_words); $crate::internal_macros::impl_array_newtype!($name, u64, $n_words);
impl $name { impl $name {
/// Conversion to u32 /// Conversion to u32
@ -404,7 +404,7 @@ macro_rules! construct_uint {
} }
} }
display_from_debug!($name); $crate::internal_macros::display_from_debug!($name);
impl $crate::consensus::Encodable for $name { impl $crate::consensus::Encodable for $name {
#[inline] #[inline]