Skip to content

Commit 9530313

Browse files
committed
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.
1 parent 768c693 commit 9530313

29 files changed

+49
-8
lines changed

src/blockdata/block.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::blockdata::transaction::Transaction;
2525
use crate::blockdata::constants::{max_target, WITNESS_SCALE_FACTOR};
2626
use crate::blockdata::script;
2727
use crate::VarInt;
28+
use crate::internal_macros::impl_consensus_encoding;
2829

2930
/// Bitcoin block header.
3031
///

src/blockdata/constants.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::blockdata::block::{Block, BlockHeader};
2121
use crate::blockdata::witness::Witness;
2222
use crate::network::constants::Network;
2323
use crate::util::uint::Uint256;
24+
use crate::internal_macros::{impl_array_newtype, impl_bytes_newtype};
2425

2526
/// The maximum allowable sequence number
2627
pub const MAX_SEQUENCE: u32 = 0xFFFFFFFF;

src/blockdata/opcodes.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#[cfg(feature = "serde")] use crate::prelude::*;
1515

1616
use core::{fmt, convert::From};
17+
use crate::internal_macros::display_from_debug;
1718

1819
// Note: I am deliberately not implementing PartialOrd or Ord on the
1920
// opcode enum. If you want to check ranges of opcodes, etc.,

src/blockdata/script.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::io;
1818
use core::convert::TryFrom;
1919
use core::{fmt, default::Default};
2020
use core::ops::Index;
21+
use crate::internal_macros::display_from_debug;
2122

2223
#[cfg(feature = "serde")] use serde;
2324

@@ -1092,6 +1093,7 @@ mod test {
10921093
use crate::blockdata::opcodes;
10931094
use crate::util::key::PublicKey;
10941095
use crate::util::psbt::serialize::Serialize;
1096+
use crate::internal_macros::hex_script;
10951097

10961098
#[test]
10971099
fn script() {

src/blockdata/transaction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use crate::consensus::{encode, Decodable, Encodable};
3030
use crate::hash_types::{Sighash, Txid, Wtxid};
3131
use crate::VarInt;
3232
use crate::util::sighash::UINT256_ONE;
33+
use crate::internal_macros::{impl_consensus_encoding, serde_string_impl, serde_struct_human_string_impl, write_err};
3334

3435
#[cfg(doc)]
3536
use crate::util::sighash::SchnorrSighashType;

src/consensus/encode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::{fmt, mem, u32, convert::From};
2222

2323
use crate::hashes::{sha256d, Hash, sha256};
2424
use crate::hash_types::{BlockHash, FilterHash, TxMerkleNode, FilterHeader};
25-
25+
use crate::internal_macros::write_err;
2626
use crate::io::{self, Cursor, Read};
2727

2828
use crate::util::endian;
@@ -231,7 +231,7 @@ macro_rules! decoder_fn {
231231
($name:ident, $val_type:ty, $readfn:ident, $byte_len: expr) => {
232232
#[inline]
233233
fn $name(&mut self) -> Result<$val_type, Error> {
234-
const_assert!(::core::mem::size_of::<$val_type>() == $byte_len);
234+
$crate::internal_macros::const_assert!(::core::mem::size_of::<$val_type>() == $byte_len);
235235
let mut val = [0; $byte_len];
236236
self.read_exact(&mut val[..]).map_err(Error::Io)?;
237237
Ok(endian::$readfn(&val))

src/internal_macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ macro_rules! impl_consensus_encoding {
4444
}
4545
);
4646
}
47+
pub(crate) use impl_consensus_encoding;
4748

4849
/// Implements standard array methods for a given wrapper type
4950
macro_rules! impl_array_newtype {
@@ -104,6 +105,7 @@ macro_rules! impl_array_newtype {
104105
}
105106
};
106107
}
108+
pub(crate) use impl_array_newtype;
107109

108110
macro_rules! display_from_debug {
109111
($thing:ident) => {
@@ -114,15 +116,22 @@ macro_rules! display_from_debug {
114116
}
115117
};
116118
}
119+
pub(crate) use display_from_debug;
117120

118121
#[cfg(test)]
119122
macro_rules! hex_script (($s:expr) => (<$crate::Script as core::str::FromStr>::from_str($s).unwrap()));
123+
#[cfg(test)]
124+
pub(crate) use hex_script;
120125

121126
#[cfg(test)]
122127
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()));
128+
#[cfg(test)]
129+
pub(crate) use hex_hash;
123130

124131
#[cfg(test)]
125132
macro_rules! hex_decode (($h:ident, $s:expr) => (deserialize::<$h>(&<$crate::prelude::Vec<u8> as $crate::hashes::hex::FromHex>::from_hex($s).unwrap()).unwrap()));
133+
#[cfg(test)]
134+
pub(crate) use hex_decode;
126135

127136
macro_rules! serde_string_impl {
128137
($name:ident, $expecting:literal) => {
@@ -168,6 +177,7 @@ macro_rules! serde_string_impl {
168177
}
169178
};
170179
}
180+
pub(crate) use serde_string_impl;
171181

172182
/// A combination macro where the human-readable serialization is done like
173183
/// 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 {
342352
}
343353
)
344354
}
355+
pub(crate) use serde_struct_human_string_impl;
345356

346357
/// Implements several traits for byte-based newtypes.
347358
/// Implements:
@@ -476,6 +487,7 @@ macro_rules! impl_bytes_newtype {
476487
}
477488
};
478489
}
490+
pub(crate) use impl_bytes_newtype;
479491

480492
macro_rules! user_enum {
481493
(
@@ -563,6 +575,7 @@ macro_rules! user_enum {
563575
}
564576
);
565577
}
578+
pub(crate) use user_enum;
566579

567580
/// Formats error. If `std` feature is OFF appends error source (delimited by `: `). We do this
568581
/// because `e.source()` is only available in std builds, without this macro the error source is
@@ -582,10 +595,12 @@ macro_rules! write_err {
582595
}
583596
}
584597
}
598+
pub(crate) use write_err;
585599

586600
/// Asserts a boolean expression at compile time.
587601
macro_rules! const_assert {
588602
($x:expr) => {{
589603
const _: [(); 0 - !$x as usize] = [];
590604
}};
591605
}
606+
pub(crate) use const_assert;

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ extern crate actual_serde as serde;
7575
#[cfg(test)]
7676
#[macro_use]
7777
mod test_macros;
78-
#[macro_use]
7978
mod internal_macros;
8079
#[cfg(feature = "serde")]
8180
mod serde_utils;

src/network/constants.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use core::{fmt, ops, convert::From};
3030

3131
use crate::io;
3232
use crate::consensus::encode::{self, Encodable, Decodable};
33+
use crate::internal_macros::user_enum;
3334

3435
/// Version of the protocol as appearing in network message headers
3536
/// This constant is used to signal to other peers which features you support.

src/network/message_blockdata.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::hashes::{Hash as _, sha256d};
1616
use crate::network::constants;
1717
use crate::consensus::encode::{self, Decodable, Encodable};
1818
use crate::hash_types::{BlockHash, Txid, Wtxid};
19+
use crate::internal_macros::impl_consensus_encoding;
1920

2021
/// An inventory item.
2122
#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash, PartialOrd, Ord)]

0 commit comments

Comments
 (0)